blob: 48b4ea404695e3ce79367a5cb3193c22b6f9ef51 [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
Ernie Rael18143d32023-09-04 22:30:41 +0200139generate_GET_OBJ_MEMBER(cctx_T *cctx, int idx, type_T *type, int is_static)
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;
150 isn->isn_arg.classmember.cm_static = is_static;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000151 return push_type_stack2(cctx, type, &t_any);
152}
153
154/*
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000155 * Generate ISN_GET_ITF_MEMBER - access member of interface at bottom of stack
156 * by index.
157 */
158 int
Ernie Rael18143d32023-09-04 22:30:41 +0200159generate_GET_ITF_MEMBER(cctx_T *cctx, class_T *itf, int idx, type_T *type,
160 int is_static)
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000161{
162 RETURN_OK_IF_SKIP(cctx);
163
164 // drop the object type
165 isn_T *isn = generate_instr_drop(cctx, ISN_GET_ITF_MEMBER, 1);
166 if (isn == NULL)
167 return FAIL;
168
169 isn->isn_arg.classmember.cm_class = itf;
170 ++itf->class_refcount;
171 isn->isn_arg.classmember.cm_idx = idx;
Ernie Rael18143d32023-09-04 22:30:41 +0200172 isn->isn_arg.classmember.cm_static = is_static;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000173 return push_type_stack2(cctx, type, &t_any);
174}
175
176/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000177 * Generate ISN_STORE_THIS - store value in member of "this" object with member
178 * index "idx".
179 */
180 int
181generate_STORE_THIS(cctx_T *cctx, int idx)
182{
183 RETURN_OK_IF_SKIP(cctx);
184
185 // drop the value type
186 isn_T *isn = generate_instr_drop(cctx, ISN_STORE_THIS, 1);
187 if (isn == NULL)
188 return FAIL;
189
190 isn->isn_arg.number = idx;
191 return OK;
192}
193
194/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000195 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
196 * But only for simple types.
197 * When "tolerant" is TRUE convert most types to string, e.g. a List.
198 */
199 int
200may_generate_2STRING(int offset, int tolerant, cctx_T *cctx)
201{
202 isn_T *isn;
203 isntype_T isntype = ISN_2STRING;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000204 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000205
206 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +0000207 type = get_type_on_stack(cctx, -1 - offset);
208 switch (type->tt_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000209 {
210 // nothing to be done
211 case VAR_STRING: return OK;
212
213 // conversion possible
214 case VAR_SPECIAL:
215 case VAR_BOOL:
216 case VAR_NUMBER:
217 case VAR_FLOAT:
218 break;
219
220 // conversion possible (with runtime check)
221 case VAR_ANY:
222 case VAR_UNKNOWN:
223 isntype = ISN_2STRING_ANY;
224 break;
225
226 // conversion possible when tolerant
227 case VAR_LIST:
228 if (tolerant)
229 {
230 isntype = ISN_2STRING_ANY;
231 break;
232 }
233 // FALLTHROUGH
234
235 // conversion not possible
236 case VAR_VOID:
237 case VAR_BLOB:
238 case VAR_FUNC:
239 case VAR_PARTIAL:
240 case VAR_DICT:
241 case VAR_JOB:
242 case VAR_CHANNEL:
243 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000244 case VAR_CLASS:
245 case VAR_OBJECT:
Bram Moolenaar078a4612022-01-04 15:17:03 +0000246 to_string_error(type->tt_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000247 return FAIL;
248 }
249
Bram Moolenaar078a4612022-01-04 15:17:03 +0000250 set_type_on_stack(cctx, &t_string, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000251 if ((isn = generate_instr(cctx, isntype)) == NULL)
252 return FAIL;
253 isn->isn_arg.tostring.offset = offset;
254 isn->isn_arg.tostring.tolerant = tolerant;
255
256 return OK;
257}
258
259 static int
260check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
261{
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000262 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT
263 || type1 == VAR_ANY || type1 == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000264 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000265 || type2 == VAR_ANY || type2 == VAR_UNKNOWN)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000266 {
267 if (*op == '+')
268 emsg(_(e_wrong_argument_type_for_plus));
269 else
270 semsg(_(e_char_requires_number_or_float_arguments), *op);
271 return FAIL;
272 }
273 return OK;
274}
275
276/*
277 * Generate instruction for "+". For a list this creates a new list.
278 */
279 int
280generate_add_instr(
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000281 cctx_T *cctx,
282 vartype_T vartype,
283 type_T *type1,
284 type_T *type2,
285 exprtype_T expr_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000286{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000287 isn_T *isn = generate_instr_drop(cctx,
288 vartype == VAR_NUMBER ? ISN_OPNR
289 : vartype == VAR_LIST ? ISN_ADDLIST
290 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000291 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000292 : ISN_OPANY, 1);
293
294 if (vartype != VAR_LIST && vartype != VAR_BLOB
295 && type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000296 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000297 && type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000298 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000299 && check_number_or_float(
300 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
301 return FAIL;
302
303 if (isn != NULL)
304 {
305 if (isn->isn_type == ISN_ADDLIST)
306 isn->isn_arg.op.op_type = expr_type;
307 else
308 isn->isn_arg.op.op_type = EXPR_ADD;
309 }
310
311 // When concatenating two lists with different member types the member type
312 // becomes "any".
313 if (vartype == VAR_LIST
314 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
315 && type1->tt_member != type2->tt_member)
Bram Moolenaar078a4612022-01-04 15:17:03 +0000316 set_type_on_stack(cctx, &t_list_any, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000317
318 return isn == NULL ? FAIL : OK;
319}
320
321/*
322 * Get the type to use for an instruction for an operation on "type1" and
323 * "type2". If they are matching use a type-specific instruction. Otherwise
324 * fall back to runtime type checking.
325 */
326 vartype_T
327operator_type(type_T *type1, type_T *type2)
328{
329 if (type1->tt_type == type2->tt_type
330 && (type1->tt_type == VAR_NUMBER
331 || type1->tt_type == VAR_LIST
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000332 || type1->tt_type == VAR_FLOAT
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000333 || type1->tt_type == VAR_BLOB))
334 return type1->tt_type;
335 return VAR_ANY;
336}
337
338/*
339 * Generate an instruction with two arguments. The instruction depends on the
340 * type of the arguments.
341 */
342 int
343generate_two_op(cctx_T *cctx, char_u *op)
344{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000345 type_T *type1;
346 type_T *type2;
347 vartype_T vartype;
348 isn_T *isn;
349
350 RETURN_OK_IF_SKIP(cctx);
351
352 // Get the known type of the two items on the stack.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000353 type1 = get_type_on_stack(cctx, 1);
354 type2 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000355 vartype = operator_type(type1, type2);
356
357 switch (*op)
358 {
359 case '+':
360 if (generate_add_instr(cctx, vartype, type1, type2,
361 EXPR_COPY) == FAIL)
362 return FAIL;
363 break;
364
365 case '-':
366 case '*':
367 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
368 op) == FAIL)
369 return FAIL;
370 if (vartype == VAR_NUMBER)
371 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000372 else if (vartype == VAR_FLOAT)
373 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000374 else
375 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
376 if (isn != NULL)
377 isn->isn_arg.op.op_type = *op == '*'
378 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
379 break;
380
381 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000382 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000383 && type1->tt_type != VAR_NUMBER)
384 || (type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000385 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000386 && type2->tt_type != VAR_NUMBER))
387 {
388 emsg(_(e_percent_requires_number_arguments));
389 return FAIL;
390 }
391 isn = generate_instr_drop(cctx,
392 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
393 if (isn != NULL)
394 isn->isn_arg.op.op_type = EXPR_REM;
395 break;
396 }
397
398 // correct type of result
399 if (vartype == VAR_ANY)
400 {
401 type_T *type = &t_any;
402
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000403 // float+number and number+float results in float
404 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100405 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000406 type = &t_float;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000407 set_type_on_stack(cctx, type, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000408 }
409
410 return OK;
411}
412
413/*
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000414 * Get the instruction to use for comparing two values with specified types.
415 * Either "tv1" and "tv2" are passed or "type1" and "type2".
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000416 * Return ISN_DROP when failed.
417 */
418 static isntype_T
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000419get_compare_isn(
Bram Moolenaar2ed57ac2023-04-01 22:05:38 +0100420 exprtype_T exprtype,
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000421 typval_T *tv1,
422 typval_T *tv2,
423 type_T *type1,
424 type_T *type2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000425{
426 isntype_T isntype = ISN_DROP;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000427 vartype_T vartype1 = tv1 != NULL ? tv1->v_type : type1->tt_type;
428 vartype_T vartype2 = tv2 != NULL ? tv2->v_type : type2->tt_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000429
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000430 if (vartype1 == vartype2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000431 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000432 switch (vartype1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000433 {
434 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
435 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
436 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
437 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
438 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
439 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
440 case VAR_LIST: isntype = ISN_COMPARELIST; break;
441 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
442 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000443 case VAR_CLASS: isntype = ISN_COMPARECLASS; break;
444 case VAR_OBJECT: isntype = ISN_COMPAREOBJECT; break;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000445 default: isntype = ISN_COMPAREANY; break;
446 }
447 }
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000448 else if (vartype1 == VAR_ANY || vartype2 == VAR_ANY
449 || ((vartype1 == VAR_NUMBER || vartype1 == VAR_FLOAT)
450 && (vartype2 == VAR_NUMBER || vartype2 == VAR_FLOAT))
451 || (vartype1 == VAR_FUNC && vartype2 == VAR_PARTIAL)
452 || (vartype1 == VAR_PARTIAL && vartype2 == VAR_FUNC))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000453 isntype = ISN_COMPAREANY;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000454 else if (vartype1 == VAR_SPECIAL || vartype2 == VAR_SPECIAL)
Bram Moolenaar7a222242022-03-01 19:23:24 +0000455 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000456 if ((vartype1 == VAR_SPECIAL
457 && (tv1 != NULL ? tv1->vval.v_number == VVAL_NONE
458 : type1 == &t_none)
459 && vartype2 != VAR_STRING)
460 || (vartype2 == VAR_SPECIAL
461 && (tv2 != NULL ? tv2->vval.v_number == VVAL_NONE
462 : type2 == &t_none)
463 && vartype1 != VAR_STRING))
464 {
465 semsg(_(e_cannot_compare_str_with_str),
466 vartype_name(vartype1), vartype_name(vartype2));
467 return ISN_DROP;
468 }
Bram Moolenaar05667812022-03-15 20:21:33 +0000469 // although comparing null with number, float or bool is not useful, we
470 // allow it
Bram Moolenaar7a222242022-03-01 19:23:24 +0000471 isntype = ISN_COMPARENULL;
472 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000473
474 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
475 && (isntype == ISN_COMPAREBOOL
476 || isntype == ISN_COMPARESPECIAL
477 || isntype == ISN_COMPARENR
478 || isntype == ISN_COMPAREFLOAT))
479 {
480 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000481 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(vartype1));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000482 return ISN_DROP;
483 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000484 if (!(exprtype == EXPR_IS || exprtype == EXPR_ISNOT
485 || exprtype == EXPR_EQUAL || exprtype == EXPR_NEQUAL)
486 && (isntype == ISN_COMPAREOBJECT || isntype == ISN_COMPARECLASS))
487 {
488 semsg(_(e_invalid_operation_for_str), vartype_name(vartype1));
489 return ISN_DROP;
490 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000491 if (isntype == ISN_DROP
Bram Moolenaar2ed57ac2023-04-01 22:05:38 +0100492 || (isntype != ISN_COMPARENULL
493 && (((exprtype != EXPR_EQUAL
494 && exprtype != EXPR_NEQUAL
495 && (vartype1 == VAR_BOOL || vartype1 == VAR_SPECIAL
496 || vartype2 == VAR_BOOL || vartype2 == VAR_SPECIAL)))
497 || ((exprtype != EXPR_EQUAL
498 && exprtype != EXPR_NEQUAL
499 && exprtype != EXPR_IS
500 && exprtype != EXPR_ISNOT
501 && (vartype1 == VAR_BLOB || vartype2 == VAR_BLOB
502 || vartype1 == VAR_LIST || vartype2 == VAR_LIST))))))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000503 {
504 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000505 vartype_name(vartype1), vartype_name(vartype2));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000506 return ISN_DROP;
507 }
508 return isntype;
509}
510
511 int
512check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
513{
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000514 if (get_compare_isn(type, tv1, tv2, NULL, NULL) == ISN_DROP)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000515 return FAIL;
516 return OK;
517}
518
519/*
520 * Generate an ISN_COMPARE* instruction with a boolean result.
521 */
522 int
523generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
524{
525 isntype_T isntype;
526 isn_T *isn;
527 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000528
529 RETURN_OK_IF_SKIP(cctx);
530
531 // Get the known type of the two items on the stack. If they are matching
532 // use a type-specific instruction. Otherwise fall back to runtime type
533 // checking.
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000534 isntype = get_compare_isn(exprtype, NULL, NULL,
535 get_type_on_stack(cctx, 1), get_type_on_stack(cctx, 0));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000536 if (isntype == ISN_DROP)
537 return FAIL;
538
539 if ((isn = generate_instr(cctx, isntype)) == NULL)
540 return FAIL;
541 isn->isn_arg.op.op_type = exprtype;
542 isn->isn_arg.op.op_ic = ic;
543
544 // takes two arguments, puts one bool back
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100545 --stack->ga_len;
546 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000547
548 return OK;
549}
550
551/*
LemonBoy372bcce2022-04-25 12:43:20 +0100552 * Generate an ISN_CONCAT instruction.
553 * "count" is the number of stack elements to join together and it must be
554 * greater or equal to one.
555 * The caller ensures all the "count" elements on the stack have the right type.
556 */
557 int
558generate_CONCAT(cctx_T *cctx, int count)
559{
560 isn_T *isn;
561 garray_T *stack = &cctx->ctx_type_stack;
562
563 RETURN_OK_IF_SKIP(cctx);
564
LemonBoy372bcce2022-04-25 12:43:20 +0100565 if ((isn = generate_instr(cctx, ISN_CONCAT)) == NULL)
566 return FAIL;
567 isn->isn_arg.number = count;
568
569 // drop the argument types
570 stack->ga_len -= count - 1;
571
572 return OK;
573}
574
575/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000576 * Generate an ISN_2BOOL instruction.
577 * "offset" is the offset in the type stack.
578 */
579 int
580generate_2BOOL(cctx_T *cctx, int invert, int offset)
581{
582 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000583
584 RETURN_OK_IF_SKIP(cctx);
585 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
586 return FAIL;
587 isn->isn_arg.tobool.invert = invert;
588 isn->isn_arg.tobool.offset = offset;
589
590 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000591 set_type_on_stack(cctx, &t_bool, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000592
593 return OK;
594}
595
596/*
597 * Generate an ISN_COND2BOOL instruction.
598 */
599 int
600generate_COND2BOOL(cctx_T *cctx)
601{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000602 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100603 if (generate_instr(cctx, ISN_COND2BOOL) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000604 return FAIL;
605
606 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000607 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000608
609 return OK;
610}
611
612 int
613generate_TYPECHECK(
614 cctx_T *cctx,
615 type_T *expected,
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000616 int number_ok, // add TTFLAG_NUMBER_OK flag
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000617 int offset,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100618 int is_var,
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000619 int argidx)
620{
621 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000622
623 RETURN_OK_IF_SKIP(cctx);
624 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
625 return FAIL;
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000626 type_T *tt;
627 if (expected->tt_type == VAR_FLOAT && number_ok)
628 {
629 // always allocate, also for static types
630 tt = ALLOC_ONE(type_T);
631 if (tt != NULL)
632 {
633 *tt = *expected;
Bram Moolenaarc4b3f642022-12-30 10:36:34 +0000634 tt->tt_flags &= ~TTFLAG_STATIC;
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000635 tt->tt_flags |= TTFLAG_NUMBER_OK;
636 }
637 }
638 else
639 tt = alloc_type(expected);
640
641 isn->isn_arg.type.ct_type = tt;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000642 isn->isn_arg.type.ct_off = (int8_T)offset;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100643 isn->isn_arg.type.ct_is_var = is_var;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000644 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
645
646 // type becomes expected
Bram Moolenaar078a4612022-01-04 15:17:03 +0000647 set_type_on_stack(cctx, expected, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000648
649 return OK;
650}
651
652 int
653generate_SETTYPE(
654 cctx_T *cctx,
655 type_T *expected)
656{
657 isn_T *isn;
658
659 RETURN_OK_IF_SKIP(cctx);
660 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
661 return FAIL;
662 isn->isn_arg.type.ct_type = alloc_type(expected);
663 return OK;
664}
665
666/*
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000667 * Generate an ISN_PUSHOBJ instruction. Object is always NULL.
668 */
Ernie Rael5c018be2023-08-27 18:40:26 +0200669 int
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000670generate_PUSHOBJ(cctx_T *cctx)
671{
672 RETURN_OK_IF_SKIP(cctx);
Ernie Rael5c018be2023-08-27 18:40:26 +0200673 if (generate_instr_type(cctx, ISN_PUSHOBJ, &t_object) == NULL)
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000674 return FAIL;
675 return OK;
676}
677
678/*
679 * Generate an ISN_PUSHCLASS instruction. "class" can be NULL.
680 */
681 static int
682generate_PUSHCLASS(cctx_T *cctx, class_T *class)
683{
684 RETURN_OK_IF_SKIP(cctx);
685 isn_T *isn = generate_instr_type(cctx, ISN_PUSHCLASS,
686 class == NULL ? &t_any : &class->class_type);
687 if (isn == NULL)
688 return FAIL;
Bram Moolenaar30a84472023-02-27 08:07:14 +0000689 isn->isn_arg.classarg = class;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000690 if (class != NULL)
691 ++class->class_refcount;
692 return OK;
693}
694
695/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000696 * Generate a PUSH instruction for "tv".
697 * "tv" will be consumed or cleared.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000698 */
699 int
700generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
701{
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100702 switch (tv->v_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000703 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100704 case VAR_BOOL:
705 generate_PUSHBOOL(cctx, tv->vval.v_number);
706 break;
707 case VAR_SPECIAL:
708 generate_PUSHSPEC(cctx, tv->vval.v_number);
709 break;
710 case VAR_NUMBER:
711 generate_PUSHNR(cctx, tv->vval.v_number);
712 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100713 case VAR_FLOAT:
714 generate_PUSHF(cctx, tv->vval.v_float);
715 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100716 case VAR_BLOB:
717 generate_PUSHBLOB(cctx, tv->vval.v_blob);
718 tv->vval.v_blob = NULL;
719 break;
720 case VAR_LIST:
721 if (tv->vval.v_list != NULL)
722 iemsg("non-empty list constant not supported");
723 generate_NEWLIST(cctx, 0, TRUE);
724 break;
725 case VAR_DICT:
726 if (tv->vval.v_dict != NULL)
727 iemsg("non-empty dict constant not supported");
728 generate_NEWDICT(cctx, 0, TRUE);
729 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000730#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100731 case VAR_JOB:
732 if (tv->vval.v_job != NULL)
733 iemsg("non-null job constant not supported");
734 generate_PUSHJOB(cctx);
735 break;
736 case VAR_CHANNEL:
737 if (tv->vval.v_channel != NULL)
738 iemsg("non-null channel constant not supported");
739 generate_PUSHCHANNEL(cctx);
740 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000741#endif
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100742 case VAR_FUNC:
743 if (tv->vval.v_string != NULL)
744 iemsg("non-null function constant not supported");
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100745 generate_PUSHFUNC(cctx, NULL, &t_func_unknown, TRUE);
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100746 break;
747 case VAR_PARTIAL:
748 if (tv->vval.v_partial != NULL)
749 iemsg("non-null partial constant not supported");
750 if (generate_instr_type(cctx, ISN_NEWPARTIAL, &t_func_unknown)
751 == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000752 return FAIL;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100753 break;
754 case VAR_STRING:
755 generate_PUSHS(cctx, &tv->vval.v_string);
756 tv->vval.v_string = NULL;
757 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000758 case VAR_OBJECT:
759 if (tv->vval.v_object != NULL)
760 {
761 emsg(_(e_cannot_use_non_null_object));
762 return FAIL;
763 }
764 generate_PUSHOBJ(cctx);
765 break;
766 case VAR_CLASS:
767 generate_PUSHCLASS(cctx, tv->vval.v_class);
768 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100769 default:
770 siemsg("constant type %d not supported", tv->v_type);
771 clear_tv(tv);
772 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000773 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100774 tv->v_type = VAR_UNKNOWN;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000775 return OK;
776}
777
778/*
779 * Generate an ISN_PUSHNR instruction.
780 */
781 int
782generate_PUSHNR(cctx_T *cctx, varnumber_T number)
783{
784 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000785
786 RETURN_OK_IF_SKIP(cctx);
787 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
788 return FAIL;
789 isn->isn_arg.number = number;
790
791 if (number == 0 || number == 1)
792 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000793 set_type_on_stack(cctx, &t_number_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000794 return OK;
795}
796
797/*
798 * Generate an ISN_PUSHBOOL instruction.
799 */
800 int
801generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
802{
803 isn_T *isn;
804
805 RETURN_OK_IF_SKIP(cctx);
806 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
807 return FAIL;
808 isn->isn_arg.number = number;
809
810 return OK;
811}
812
813/*
814 * Generate an ISN_PUSHSPEC instruction.
815 */
816 int
817generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
818{
819 isn_T *isn;
820
821 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000822 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC,
823 number == VVAL_NULL ? &t_null : &t_none)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000824 return FAIL;
825 isn->isn_arg.number = number;
826
827 return OK;
828}
829
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000830/*
831 * Generate an ISN_PUSHF instruction.
832 */
833 int
834generate_PUSHF(cctx_T *cctx, float_T fnumber)
835{
836 isn_T *isn;
837
838 RETURN_OK_IF_SKIP(cctx);
839 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
840 return FAIL;
841 isn->isn_arg.fnumber = fnumber;
842
843 return OK;
844}
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000845
846/*
847 * Generate an ISN_PUSHS instruction.
848 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100849 * Note that if "str" is used in the instruction OK is returned and "*str" is
850 * not set to NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000851 */
852 int
853generate_PUSHS(cctx_T *cctx, char_u **str)
854{
855 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100856 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000857
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100858 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000859 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100860 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
861 ret = FAIL;
862 else
863 {
864 isn->isn_arg.string = str == NULL ? NULL : *str;
865 return OK;
866 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000867 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100868 if (str != NULL)
869 VIM_CLEAR(*str);
870 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000871}
872
873/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000874 * Generate an ISN_PUSHCHANNEL instruction. Channel is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000875 */
876 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000877generate_PUSHCHANNEL(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000878{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000879 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100880#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100881 if (generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000882 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000883 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100884#else
885 emsg(_(e_channel_job_feature_not_available));
886 return FAIL;
887#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000888}
889
890/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000891 * Generate an ISN_PUSHJOB instruction. Job is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000892 */
893 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000894generate_PUSHJOB(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000895{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000896 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100897#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100898 if (generate_instr_type(cctx, ISN_PUSHJOB, &t_job) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000899 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000900 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100901#else
902 emsg(_(e_channel_job_feature_not_available));
903 return FAIL;
904#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000905}
906
907/*
908 * Generate an ISN_PUSHBLOB instruction.
909 * Consumes "blob".
910 */
911 int
912generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
913{
914 isn_T *isn;
915
916 RETURN_OK_IF_SKIP(cctx);
917 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
918 return FAIL;
919 isn->isn_arg.blob = blob;
920
921 return OK;
922}
923
924/*
925 * Generate an ISN_PUSHFUNC instruction with name "name".
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100926 * When "may_prefix" is TRUE prefix "g:" unless "name" is script-local or
927 * autoload.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000928 */
929 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100930generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type, int may_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000931{
932 isn_T *isn;
933 char_u *funcname;
934
935 RETURN_OK_IF_SKIP(cctx);
936 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
937 return FAIL;
938 if (name == NULL)
939 funcname = NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100940 else if (!may_prefix
941 || *name == K_SPECIAL // script-local
Bram Moolenaard041f422022-01-12 19:54:00 +0000942 || vim_strchr(name, AUTOLOAD_CHAR) != NULL) // autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000943 funcname = vim_strsave(name);
944 else
945 {
946 funcname = alloc(STRLEN(name) + 3);
947 if (funcname != NULL)
948 {
949 STRCPY(funcname, "g:");
950 STRCPY(funcname + 2, name);
951 }
952 }
953
954 isn->isn_arg.string = funcname;
955 return OK;
956}
957
958/*
Bram Moolenaar06b77222022-01-25 15:51:56 +0000959 * Generate an ISN_AUTOLOAD instruction.
960 */
961 int
962generate_AUTOLOAD(cctx_T *cctx, char_u *name, type_T *type)
963{
964 isn_T *isn;
965
966 RETURN_OK_IF_SKIP(cctx);
967 if ((isn = generate_instr_type(cctx, ISN_AUTOLOAD, type)) == NULL)
968 return FAIL;
969 isn->isn_arg.string = vim_strsave(name);
970 if (isn->isn_arg.string == NULL)
971 return FAIL;
972 return OK;
973}
974
975/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000976 * Generate an ISN_GETITEM instruction with "index".
977 * "with_op" is TRUE for "+=" and other operators, the stack has the current
978 * value below the list with values.
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100979 * Caller must check the type is a list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000980 */
981 int
982generate_GETITEM(cctx_T *cctx, int index, int with_op)
983{
984 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000985 type_T *type = get_type_on_stack(cctx, with_op ? 1 : 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000986 type_T *item_type = &t_any;
987
988 RETURN_OK_IF_SKIP(cctx);
989
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000990 item_type = type->tt_member;
991 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
992 return FAIL;
993 isn->isn_arg.getitem.gi_index = index;
994 isn->isn_arg.getitem.gi_with_op = with_op;
995
996 // add the item type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +0000997 return push_type_stack(cctx, item_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000998}
999
1000/*
1001 * Generate an ISN_SLICE instruction with "count".
1002 */
1003 int
1004generate_SLICE(cctx_T *cctx, int count)
1005{
1006 isn_T *isn;
1007
1008 RETURN_OK_IF_SKIP(cctx);
1009 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1010 return FAIL;
1011 isn->isn_arg.number = count;
1012 return OK;
1013}
1014
1015/*
1016 * Generate an ISN_CHECKLEN instruction with "min_len".
1017 */
1018 int
1019generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1020{
1021 isn_T *isn;
1022
1023 RETURN_OK_IF_SKIP(cctx);
1024
1025 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1026 return FAIL;
1027 isn->isn_arg.checklen.cl_min_len = min_len;
1028 isn->isn_arg.checklen.cl_more_OK = more_OK;
1029
1030 return OK;
1031}
1032
1033/*
1034 * Generate an ISN_STORE instruction.
1035 */
1036 int
1037generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1038{
1039 isn_T *isn;
1040
1041 RETURN_OK_IF_SKIP(cctx);
1042 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1043 return FAIL;
1044 if (name != NULL)
1045 isn->isn_arg.string = vim_strsave(name);
1046 else
1047 isn->isn_arg.number = idx;
1048
1049 return OK;
1050}
1051
1052/*
Bram Moolenaard505d172022-12-18 21:42:55 +00001053 * Generate an ISN_LOAD_CLASSMEMBER ("load" == TRUE) or ISN_STORE_CLASSMEMBER
1054 * ("load" == FALSE) instruction.
1055 */
1056 int
1057generate_CLASSMEMBER(
1058 cctx_T *cctx,
1059 int load,
1060 class_T *cl,
1061 int idx)
1062{
1063 isn_T *isn;
1064
1065 RETURN_OK_IF_SKIP(cctx);
1066 if (load)
1067 {
1068 ocmember_T *m = &cl->class_class_members[idx];
1069 isn = generate_instr_type(cctx, ISN_LOAD_CLASSMEMBER, m->ocm_type);
1070 }
1071 else
1072 {
1073 isn = generate_instr_drop(cctx, ISN_STORE_CLASSMEMBER, 1);
1074 }
1075 if (isn == NULL)
1076 return FAIL;
1077 isn->isn_arg.classmember.cm_class = cl;
1078 ++cl->class_refcount;
1079 isn->isn_arg.classmember.cm_idx = idx;
1080
1081 return OK;
1082}
1083
1084/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001085 * Generate an ISN_STOREOUTER instruction.
1086 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +00001087 static int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001088generate_STOREOUTER(cctx_T *cctx, int idx, int level, int loop_idx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001089{
1090 isn_T *isn;
1091
1092 RETURN_OK_IF_SKIP(cctx);
1093 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1094 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001095 if (level == 1 && loop_idx >= 0 && idx >= loop_idx)
1096 {
1097 // Store a variable defined in a loop. A copy will be made at the end
1098 // of the loop. TODO: how about deeper nesting?
1099 isn->isn_arg.outer.outer_idx = idx - loop_idx;
1100 isn->isn_arg.outer.outer_depth = OUTER_LOOP_DEPTH;
1101 }
1102 else
1103 {
1104 isn->isn_arg.outer.outer_idx = idx;
1105 isn->isn_arg.outer.outer_depth = level;
1106 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001107
1108 return OK;
1109}
1110
1111/*
1112 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1113 */
1114 int
1115generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1116{
1117 isn_T *isn;
1118
1119 RETURN_OK_IF_SKIP(cctx);
1120 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1121 return FAIL;
1122 isn->isn_arg.storenr.stnr_idx = idx;
1123 isn->isn_arg.storenr.stnr_val = value;
1124
1125 return OK;
1126}
1127
1128/*
1129 * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction
1130 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +00001131 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001132generate_STOREOPT(
1133 cctx_T *cctx,
1134 isntype_T isn_type,
1135 char_u *name,
1136 int opt_flags)
1137{
1138 isn_T *isn;
1139
1140 RETURN_OK_IF_SKIP(cctx);
1141 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1142 return FAIL;
1143 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1144 isn->isn_arg.storeopt.so_flags = opt_flags;
1145
1146 return OK;
1147}
1148
1149/*
1150 * Generate an ISN_LOAD or similar instruction.
1151 */
1152 int
1153generate_LOAD(
1154 cctx_T *cctx,
1155 isntype_T isn_type,
1156 int idx,
1157 char_u *name,
1158 type_T *type)
1159{
1160 isn_T *isn;
1161
1162 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001163 if ((isn = generate_instr_type2(cctx, isn_type, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001164 return FAIL;
1165 if (name != NULL)
1166 isn->isn_arg.string = vim_strsave(name);
1167 else
1168 isn->isn_arg.number = idx;
1169
1170 return OK;
1171}
1172
1173/*
1174 * Generate an ISN_LOADOUTER instruction
1175 */
1176 int
1177generate_LOADOUTER(
1178 cctx_T *cctx,
1179 int idx,
1180 int nesting,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001181 int loop_depth,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001182 int loop_idx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001183 type_T *type)
1184{
1185 isn_T *isn;
1186
1187 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001188 if ((isn = generate_instr_type2(cctx, ISN_LOADOUTER, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001189 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001190 if (nesting == 1 && loop_idx >= 0 && idx >= loop_idx)
1191 {
1192 // Load a variable defined in a loop. A copy will be made at the end
Bram Moolenaarcc341812022-09-19 15:54:34 +01001193 // of the loop.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001194 isn->isn_arg.outer.outer_idx = idx - loop_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001195 isn->isn_arg.outer.outer_depth = -loop_depth - 1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001196 }
1197 else
1198 {
1199 isn->isn_arg.outer.outer_idx = idx;
1200 isn->isn_arg.outer.outer_depth = nesting;
1201 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001202
1203 return OK;
1204}
1205
1206/*
1207 * Generate an ISN_LOADV instruction for v:var.
1208 */
1209 int
1210generate_LOADV(
1211 cctx_T *cctx,
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001212 char_u *name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001213{
1214 int di_flags;
1215 int vidx = find_vim_var(name, &di_flags);
1216 type_T *type;
1217
1218 RETURN_OK_IF_SKIP(cctx);
1219 if (vidx < 0)
1220 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001221 semsg(_(e_variable_not_found_str), name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001222 return FAIL;
1223 }
Bram Moolenaard787e402021-12-24 21:36:12 +00001224 type = get_vim_var_type(vidx, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001225 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
1226}
1227
1228/*
1229 * Generate an ISN_UNLET instruction.
1230 */
1231 int
1232generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
1233{
1234 isn_T *isn;
1235
1236 RETURN_OK_IF_SKIP(cctx);
1237 if ((isn = generate_instr(cctx, isn_type)) == NULL)
1238 return FAIL;
1239 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1240 isn->isn_arg.unlet.ul_forceit = forceit;
1241
1242 return OK;
1243}
1244
1245/*
1246 * Generate an ISN_LOCKCONST instruction.
1247 */
1248 int
1249generate_LOCKCONST(cctx_T *cctx)
1250{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001251 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01001252 if (generate_instr(cctx, ISN_LOCKCONST) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001253 return FAIL;
1254 return OK;
1255}
1256
1257/*
1258 * Generate an ISN_LOADS instruction.
1259 */
1260 int
1261generate_OLDSCRIPT(
1262 cctx_T *cctx,
1263 isntype_T isn_type,
1264 char_u *name,
1265 int sid,
1266 type_T *type)
1267{
1268 isn_T *isn;
1269
1270 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001271 if (isn_type == ISN_LOADS || isn_type == ISN_LOADEXPORT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001272 isn = generate_instr_type(cctx, isn_type, type);
1273 else
1274 isn = generate_instr_drop(cctx, isn_type, 1);
1275 if (isn == NULL)
1276 return FAIL;
1277 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1278 isn->isn_arg.loadstore.ls_sid = sid;
1279
1280 return OK;
1281}
1282
1283/*
1284 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1285 */
1286 int
1287generate_VIM9SCRIPT(
1288 cctx_T *cctx,
1289 isntype_T isn_type,
1290 int sid,
1291 int idx,
1292 type_T *type)
1293{
1294 isn_T *isn;
1295 scriptref_T *sref;
1296 scriptitem_T *si = SCRIPT_ITEM(sid);
1297
1298 RETURN_OK_IF_SKIP(cctx);
1299 if (isn_type == ISN_LOADSCRIPT)
Bram Moolenaar160afdb2022-02-06 17:17:02 +00001300 isn = generate_instr_type2(cctx, isn_type, type, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001301 else
1302 isn = generate_instr_drop(cctx, isn_type, 1);
1303 if (isn == NULL)
1304 return FAIL;
1305
1306 // This requires three arguments, which doesn't fit in an instruction, thus
1307 // we need to allocate a struct for this.
1308 sref = ALLOC_ONE(scriptref_T);
1309 if (sref == NULL)
1310 return FAIL;
1311 isn->isn_arg.script.scriptref = sref;
1312 sref->sref_sid = sid;
1313 sref->sref_idx = idx;
1314 sref->sref_seq = si->sn_script_seq;
1315 sref->sref_type = type;
1316 return OK;
1317}
1318
1319/*
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001320 * Generate an ISN_NEWLIST instruction for "count" items.
1321 * "use_null" is TRUE for null_list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001322 */
1323 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001324generate_NEWLIST(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001325{
1326 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001327 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001328 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001329 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001330
1331 RETURN_OK_IF_SKIP(cctx);
1332 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1333 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001334 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001335
Bram Moolenaar078a4612022-01-04 15:17:03 +00001336 // Get the member type and the declared member type from all the items on
1337 // the stack.
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001338 member_type = get_member_type_from_stack(count, 1, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001339 type = get_list_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001340 decl_type = get_list_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001341
1342 // drop the value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001343 cctx->ctx_type_stack.ga_len -= count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001344
1345 // add the list type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001346 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001347}
1348
1349/*
1350 * Generate an ISN_NEWDICT instruction.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001351 * "use_null" is TRUE for null_dict.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001352 */
1353 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001354generate_NEWDICT(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001355{
1356 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001357 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001358 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001359 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001360
1361 RETURN_OK_IF_SKIP(cctx);
1362 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1363 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001364 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001365
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001366 member_type = get_member_type_from_stack(count, 2, cctx);
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,
1390 int fi,
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001391 int *isn_idx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001392{
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001393 isn_T *isn;
1394 type_T *type;
1395 funcref_extra_T *extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001396 loopvarinfo_T loopinfo;
1397 int has_vars;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001398
1399 RETURN_OK_IF_SKIP(cctx);
1400 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1401 return FAIL;
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001402 if (isn_idx != NULL)
1403 // save the index of the new instruction
1404 *isn_idx = cctx->ctx_instr.ga_len - 1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001405
Bram Moolenaarcc341812022-09-19 15:54:34 +01001406 has_vars = get_loop_var_info(cctx, &loopinfo);
Bram Moolenaar313e4722023-02-08 20:55:27 +00001407 if (ufunc->uf_def_status == UF_NOT_COMPILED || has_vars || cl != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001408 {
1409 extra = ALLOC_CLEAR_ONE(funcref_extra_T);
1410 if (extra == NULL)
1411 return FAIL;
1412 isn->isn_arg.funcref.fr_extra = extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001413 extra->fre_loopvar_info = loopinfo;
Bram Moolenaar313e4722023-02-08 20:55:27 +00001414 if (cl != NULL)
1415 {
1416 extra->fre_class = cl;
1417 ++cl->class_refcount;
1418 extra->fre_method_idx = fi;
1419 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001420 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00001421 if (ufunc->uf_def_status == UF_NOT_COMPILED || cl != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001422 extra->fre_func_name = vim_strsave(ufunc->uf_name);
Bram Moolenaar313e4722023-02-08 20:55:27 +00001423 if (ufunc->uf_def_status != UF_NOT_COMPILED && cl == NULL)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001424 {
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001425 if (isn_idx == NULL && ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001426 // compile the function now, we need the uf_dfunc_idx value
1427 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001428 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001429 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01001430
1431 // Reserve an extra variable to keep track of the number of closures
1432 // created.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001433 cctx->ctx_has_closure = 1;
1434
1435 // If the referenced function is a closure, it may use items further up in
Bram Moolenaar139575d2022-03-15 19:29:30 +00001436 // the nested context, including this one. But not a function defined at
1437 // the script level.
1438 if ((ufunc->uf_flags & FC_CLOSURE)
1439 && func_name_refcount(cctx->ctx_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001440 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1441
Bram Moolenaar078a4612022-01-04 15:17:03 +00001442 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1443 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001444}
1445
1446/*
1447 * Generate an ISN_NEWFUNC instruction.
1448 * "lambda_name" and "func_name" must be in allocated memory and will be
1449 * consumed.
1450 */
1451 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001452generate_NEWFUNC(
1453 cctx_T *cctx,
1454 char_u *lambda_name,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001455 char_u *func_name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001456{
1457 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001458 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001459
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001460 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001461 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001462 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1463 ret = FAIL;
1464 else
1465 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001466 newfuncarg_T *arg = ALLOC_CLEAR_ONE(newfuncarg_T);
1467
1468 if (arg == NULL)
1469 ret = FAIL;
1470 else
1471 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01001472 // Reserve an extra variable to keep track of the number of
1473 // closures created.
1474 cctx->ctx_has_closure = 1;
1475
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001476 isn->isn_arg.newfunc.nf_arg = arg;
1477 arg->nfa_lambda = lambda_name;
1478 arg->nfa_global = func_name;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001479 (void)get_loop_var_info(cctx, &arg->nfa_loopvar_info);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001480 return OK;
1481 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001482 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001483 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001484 vim_free(lambda_name);
1485 vim_free(func_name);
1486 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001487}
1488
1489/*
1490 * Generate an ISN_DEF instruction: list functions
1491 */
1492 int
1493generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1494{
1495 isn_T *isn;
1496
1497 RETURN_OK_IF_SKIP(cctx);
1498 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1499 return FAIL;
1500 if (len > 0)
1501 {
1502 isn->isn_arg.string = vim_strnsave(name, len);
1503 if (isn->isn_arg.string == NULL)
1504 return FAIL;
1505 }
1506 return OK;
1507}
1508
1509/*
1510 * Generate an ISN_JUMP instruction.
1511 */
1512 int
1513generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1514{
1515 isn_T *isn;
1516 garray_T *stack = &cctx->ctx_type_stack;
1517
1518 RETURN_OK_IF_SKIP(cctx);
1519 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1520 return FAIL;
1521 isn->isn_arg.jump.jump_when = when;
1522 isn->isn_arg.jump.jump_where = where;
1523
1524 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1525 --stack->ga_len;
1526
1527 return OK;
1528}
1529
1530/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001531 * Generate an ISN_WHILE instruction. Similar to ISN_JUMP for :while
1532 */
1533 int
1534generate_WHILE(cctx_T *cctx, int funcref_idx)
1535{
1536 isn_T *isn;
1537 garray_T *stack = &cctx->ctx_type_stack;
1538
1539 RETURN_OK_IF_SKIP(cctx);
1540 if ((isn = generate_instr(cctx, ISN_WHILE)) == NULL)
1541 return FAIL;
1542 isn->isn_arg.whileloop.while_funcref_idx = funcref_idx;
1543 isn->isn_arg.whileloop.while_end = 0; // filled in later
1544
1545 if (stack->ga_len > 0)
1546 --stack->ga_len;
1547
1548 return OK;
1549}
1550
1551/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001552 * Generate an ISN_JUMP_IF_ARG_SET or ISN_JUMP_IF_ARG_NOT_SET instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001553 */
1554 int
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001555generate_JUMP_IF_ARG(cctx_T *cctx, isntype_T isn_type, int arg_off)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001556{
1557 isn_T *isn;
1558
1559 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001560 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001561 return FAIL;
1562 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1563 // jump_where is set later
1564 return OK;
1565}
1566
1567 int
1568generate_FOR(cctx_T *cctx, int loop_idx)
1569{
1570 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001571
1572 RETURN_OK_IF_SKIP(cctx);
1573 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1574 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001575 isn->isn_arg.forloop.for_loop_idx = loop_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001576
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001577 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001578 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001579}
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001580
1581 int
Bram Moolenaarcc341812022-09-19 15:54:34 +01001582generate_ENDLOOP(cctx_T *cctx, loop_info_T *loop_info)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001583{
1584 isn_T *isn;
1585
1586 RETURN_OK_IF_SKIP(cctx);
1587 if ((isn = generate_instr(cctx, ISN_ENDLOOP)) == NULL)
1588 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001589 isn->isn_arg.endloop.end_depth = loop_info->li_depth;
1590 isn->isn_arg.endloop.end_funcref_idx = loop_info->li_funcref_idx;
1591 isn->isn_arg.endloop.end_var_idx = loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001592 isn->isn_arg.endloop.end_var_count =
Bram Moolenaarcc341812022-09-19 15:54:34 +01001593 cctx->ctx_locals.ga_len - loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001594 return OK;
1595}
1596
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001597/*
1598 * Generate an ISN_TRYCONT instruction.
1599 */
1600 int
1601generate_TRYCONT(cctx_T *cctx, int levels, int where)
1602{
1603 isn_T *isn;
1604
1605 RETURN_OK_IF_SKIP(cctx);
1606 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1607 return FAIL;
1608 isn->isn_arg.trycont.tct_levels = levels;
1609 isn->isn_arg.trycont.tct_where = where;
1610
1611 return OK;
1612}
1613
Bram Moolenaar16900322022-09-08 19:51:45 +01001614/*
1615 * Check "argount" arguments and their types on the type stack.
1616 * Give an error and return FAIL if something is wrong.
1617 * When "method_call" is NULL no code is generated.
1618 */
1619 int
1620check_internal_func_args(
1621 cctx_T *cctx,
1622 int func_idx,
1623 int argcount,
1624 int method_call,
1625 type2_T **argtypes,
1626 type2_T *shuffled_argtypes)
1627{
1628 garray_T *stack = &cctx->ctx_type_stack;
1629 int argoff = check_internal_func(func_idx, argcount);
1630
1631 if (argoff < 0)
1632 return FAIL;
1633
1634 if (method_call && argoff > 1)
1635 {
Bram Moolenaarb7f22702023-04-27 16:24:07 +01001636 if (argcount < argoff)
1637 {
1638 semsg(_(e_not_enough_arguments_for_function_str),
1639 internal_func_name(func_idx));
1640 return FAIL;
1641 }
Bram Moolenaar16900322022-09-08 19:51:45 +01001642
Bram Moolenaarb7f22702023-04-27 16:24:07 +01001643 isn_T *isn = generate_instr(cctx, ISN_SHUFFLE);
Bram Moolenaar16900322022-09-08 19:51:45 +01001644 if (isn == NULL)
1645 return FAIL;
1646 isn->isn_arg.shuffle.shfl_item = argcount;
1647 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1648 }
1649
1650 if (argcount > 0)
1651 {
1652 type2_T *typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
1653
1654 // Check the types of the arguments.
1655 if (method_call && argoff > 1)
1656 {
1657 int i;
1658
1659 for (i = 0; i < argcount; ++i)
1660 shuffled_argtypes[i] = (i < argoff - 1)
1661 ? typep[i + 1]
1662 : (i == argoff - 1) ? typep[0] : typep[i];
1663 *argtypes = shuffled_argtypes;
1664 }
1665 else
1666 {
1667 int i;
1668
1669 for (i = 0; i < argcount; ++i)
1670 shuffled_argtypes[i] = typep[i];
1671 *argtypes = shuffled_argtypes;
1672 }
1673 if (internal_func_check_arg_types(*argtypes, func_idx, argcount,
1674 cctx) == FAIL)
1675 return FAIL;
1676 }
1677 return OK;
1678}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001679
1680/*
1681 * Generate an ISN_BCALL instruction.
1682 * "method_call" is TRUE for "value->method()"
1683 * Return FAIL if the number of arguments is wrong.
1684 */
1685 int
1686generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1687{
1688 isn_T *isn;
1689 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001690 type2_T *argtypes = NULL;
1691 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1692 type2_T *maptype = NULL;
1693 type_T *type;
Bram Moolenaar81330182022-02-01 12:11:58 +00001694 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001695
1696 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar16900322022-09-08 19:51:45 +01001697
1698 if (check_internal_func_args(cctx, func_idx, argcount, method_call,
1699 &argtypes, shuffled_argtypes) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001700 return FAIL;
1701
Bram Moolenaar16900322022-09-08 19:51:45 +01001702 if (internal_func_is_map(func_idx))
1703 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001704
1705 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1706 return FAIL;
1707 isn->isn_arg.bfunc.cbf_idx = func_idx;
1708 isn->isn_arg.bfunc.cbf_argcount = argcount;
1709
1710 // Drop the argument types and push the return type.
1711 stack->ga_len -= argcount;
Bram Moolenaar32517c42023-01-15 18:17:12 +00001712 type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type,
1713 cctx->ctx_type_list);
Bram Moolenaar81330182022-02-01 12:11:58 +00001714 if (push_type_stack2(cctx, type, decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001715 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001716
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001717 if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
1718 && maptype[0].type_decl->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001719 // Check that map() didn't change the item types.
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001720 generate_TYPECHECK(cctx, maptype[0].type_decl, FALSE, -1, FALSE, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001721
1722 return OK;
1723}
1724
1725/*
1726 * Generate an ISN_LISTAPPEND instruction. Works like add().
1727 * Argument count is already checked.
1728 */
1729 int
1730generate_LISTAPPEND(cctx_T *cctx)
1731{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001732 type_T *list_type;
1733 type_T *item_type;
1734 type_T *expected;
1735
1736 // Caller already checked that list_type is a list.
Bram Moolenaar81330182022-02-01 12:11:58 +00001737 // For checking the item type we use the declared type of the list and the
1738 // current type of the added item, adding a string to [1, 2] is OK.
1739 list_type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaarfa103972022-09-29 19:14:42 +01001740 if (arg_type_modifiable(list_type, 1) == FAIL)
1741 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001742 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001743 expected = list_type->tt_member;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001744 if (need_type(item_type, expected, FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001745 return FAIL;
1746
1747 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1748 return FAIL;
1749
Bram Moolenaar078a4612022-01-04 15:17:03 +00001750 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001751 return OK;
1752}
1753
1754/*
1755 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1756 * Argument count is already checked.
1757 */
1758 int
1759generate_BLOBAPPEND(cctx_T *cctx)
1760{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001761 type_T *item_type;
1762
Bram Moolenaarfa103972022-09-29 19:14:42 +01001763 // Caller already checked that blob_type is a blob, check it is modifiable.
1764 if (arg_type_modifiable(get_decl_type_on_stack(cctx, 1), 1) == FAIL)
1765 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001766 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001767 if (need_type(item_type, &t_number, FALSE,
1768 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001769 return FAIL;
1770
1771 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1772 return FAIL;
1773
Bram Moolenaar078a4612022-01-04 15:17:03 +00001774 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001775 return OK;
1776}
1777
1778/*
Bram Moolenaard0200c82023-01-28 15:19:40 +00001779 * Generate an ISN_DCALL, ISN_UCALL or ISN_METHODCALL instruction.
1780 * When calling a method on an object, of which we know the interface only,
1781 * then "cl" is the interface and "mi" the method index on the interface.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001782 * Return FAIL if the number of arguments is wrong.
1783 */
1784 int
Bram Moolenaard0200c82023-01-28 15:19:40 +00001785generate_CALL(
1786 cctx_T *cctx,
1787 ufunc_T *ufunc,
1788 class_T *cl,
1789 int mi,
1790 int pushed_argcount)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001791{
1792 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001793 int regular_args = ufunc->uf_args.ga_len;
1794 int argcount = pushed_argcount;
1795
1796 RETURN_OK_IF_SKIP(cctx);
1797 if (argcount > regular_args && !has_varargs(ufunc))
1798 {
1799 semsg(_(e_too_many_arguments_for_function_str),
1800 printable_func_name(ufunc));
1801 return FAIL;
1802 }
1803 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1804 {
1805 semsg(_(e_not_enough_arguments_for_function_str),
1806 printable_func_name(ufunc));
1807 return FAIL;
1808 }
1809
1810 if (ufunc->uf_def_status != UF_NOT_COMPILED
1811 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1812 {
1813 int i;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001814 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001815
1816 for (i = 0; i < argcount; ++i)
1817 {
1818 type_T *expected;
1819 type_T *actual;
1820
Bram Moolenaar078a4612022-01-04 15:17:03 +00001821 actual = get_type_on_stack(cctx, argcount - i - 1);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001822 if (actual->tt_type == VAR_SPECIAL
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001823 && i >= regular_args - ufunc->uf_def_args.ga_len)
1824 {
1825 // assume v:none used for default argument value
1826 continue;
1827 }
1828 if (i < regular_args)
1829 {
1830 if (ufunc->uf_arg_types == NULL)
1831 continue;
1832 expected = ufunc->uf_arg_types[i];
1833 }
1834 else if (ufunc->uf_va_type == NULL
1835 || ufunc->uf_va_type == &t_list_any)
1836 // possibly a lambda or "...: any"
1837 expected = &t_any;
1838 else
1839 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001840 if (need_type(actual, expected, FALSE,
1841 -argcount + i, i + 1, cctx, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001842 {
1843 arg_type_mismatch(expected, actual, i + 1);
1844 return FAIL;
1845 }
1846 }
Bram Moolenaar139575d2022-03-15 19:29:30 +00001847 compile_type = get_compile_type(ufunc);
1848 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001849 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaar139575d2022-03-15 19:29:30 +00001850 compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001851 return FAIL;
1852 }
1853 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1854 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001855 emsg_funcname(e_call_to_function_that_failed_to_compile_str,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001856 ufunc->uf_name);
1857 return FAIL;
1858 }
1859
Bram Moolenaard0200c82023-01-28 15:19:40 +00001860 if ((isn = generate_instr(cctx, cl != NULL ? ISN_METHODCALL
1861 : ufunc->uf_def_status != UF_NOT_COMPILED
1862 ? ISN_DCALL : ISN_UCALL)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001863 return FAIL;
Bram Moolenaar836137d2023-01-29 14:11:24 +00001864 if (cl != NULL /* isn->isn_type == ISN_METHODCALL */)
Bram Moolenaard0200c82023-01-28 15:19:40 +00001865 {
1866 isn->isn_arg.mfunc = ALLOC_ONE(cmfunc_T);
1867 if (isn->isn_arg.mfunc == NULL)
1868 return FAIL;
1869 isn->isn_arg.mfunc->cmf_itf = cl;
1870 ++cl->class_refcount;
1871 isn->isn_arg.mfunc->cmf_idx = mi;
1872 isn->isn_arg.mfunc->cmf_argcount = argcount;
1873 }
1874 else if (isn->isn_type == ISN_DCALL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001875 {
1876 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1877 isn->isn_arg.dfunc.cdf_argcount = argcount;
1878 }
1879 else
1880 {
1881 // A user function may be deleted and redefined later, can't use the
1882 // ufunc pointer, need to look it up again at runtime.
1883 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1884 isn->isn_arg.ufunc.cuf_argcount = argcount;
1885 }
1886
Bram Moolenaar078a4612022-01-04 15:17:03 +00001887 // drop the argument types
1888 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001889
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02001890 // For an object or class method call, drop the object/class type.
Yegappan Lakshmanan639751d2023-08-27 19:23:37 +02001891 if (ufunc->uf_class != NULL)
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02001892 {
1893 // When a class method is called without the class name prefix, then
1894 // the type will not be in the stack.
1895 type_T *stype = get_type_on_stack(cctx, 0);
1896 if (stype->tt_type == VAR_CLASS || stype->tt_type == VAR_OBJECT)
1897 cctx->ctx_type_stack.ga_len--;
1898 }
Yegappan Lakshmanan639751d2023-08-27 19:23:37 +02001899
Bram Moolenaar078a4612022-01-04 15:17:03 +00001900 // add return type
1901 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001902}
1903
1904/*
1905 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1906 */
1907 int
1908generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1909{
1910 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001911
1912 RETURN_OK_IF_SKIP(cctx);
1913 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1914 return FAIL;
1915 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1916 isn->isn_arg.ufunc.cuf_argcount = argcount;
1917
Bram Moolenaar078a4612022-01-04 15:17:03 +00001918 // drop the argument types
1919 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001920
Bram Moolenaar078a4612022-01-04 15:17:03 +00001921 // add return value
1922 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001923}
1924
1925/*
Bram Moolenaar16900322022-09-08 19:51:45 +01001926 * Check the arguments of function "type" against the types on the stack.
1927 * Returns OK or FAIL;
1928 */
1929 int
1930check_func_args_from_type(
1931 cctx_T *cctx,
1932 type_T *type,
1933 int argcount,
1934 int at_top,
1935 char_u *name)
1936{
1937 if (type->tt_argcount != -1)
1938 {
1939 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1940
1941 if (argcount < type->tt_min_argcount - varargs)
1942 {
1943 emsg_funcname(e_not_enough_arguments_for_function_str, name);
1944 return FAIL;
1945 }
1946 if (!varargs && argcount > type->tt_argcount)
1947 {
1948 emsg_funcname(e_too_many_arguments_for_function_str, name);
1949 return FAIL;
1950 }
1951 if (type->tt_args != NULL)
1952 {
1953 int i;
1954
1955 for (i = 0; i < argcount; ++i)
1956 {
1957 int offset = -argcount + i - (at_top ? 0 : 1);
1958 type_T *actual = get_type_on_stack(cctx, -1 - offset);
1959 type_T *expected;
1960
1961 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaar36818a92023-01-03 12:33:26 +00001962 {
1963 expected = type->tt_args[type->tt_argcount - 1];
1964 if (expected != NULL && expected->tt_type == VAR_LIST)
1965 expected = expected->tt_member;
1966 if (expected == NULL)
1967 expected = &t_any;
1968 }
Bram Moolenaar16900322022-09-08 19:51:45 +01001969 else if (i >= type->tt_min_argcount
1970 && actual->tt_type == VAR_SPECIAL)
1971 expected = &t_any;
1972 else
1973 expected = type->tt_args[i];
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001974 if (need_type(actual, expected, FALSE,
1975 offset, i + 1, cctx, TRUE, FALSE) == FAIL)
Bram Moolenaar16900322022-09-08 19:51:45 +01001976 {
1977 arg_type_mismatch(expected, actual, i + 1);
1978 return FAIL;
1979 }
1980 }
1981 }
1982 }
1983
1984 return OK;
1985}
1986/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001987 * Generate an ISN_PCALL instruction.
1988 * "type" is the type of the FuncRef.
1989 */
1990 int
1991generate_PCALL(
1992 cctx_T *cctx,
1993 int argcount,
1994 char_u *name,
1995 type_T *type,
1996 int at_top)
1997{
1998 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001999 type_T *ret_type;
2000
2001 RETURN_OK_IF_SKIP(cctx);
2002
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002003 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002004 ret_type = &t_any;
2005 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
2006 {
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002007 if (check_func_args_from_type(cctx, type, argcount, at_top, name)
2008 == FAIL)
Bram Moolenaar16900322022-09-08 19:51:45 +01002009 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002010
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002011 ret_type = type->tt_member;
2012 if (ret_type == &t_unknown)
2013 // return type not known yet, use a runtime check
2014 ret_type = &t_any;
2015 }
2016 else
2017 {
2018 semsg(_(e_not_callable_type_str), name);
2019 return FAIL;
2020 }
2021
2022 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2023 return FAIL;
2024 isn->isn_arg.pfunc.cpf_top = at_top;
2025 isn->isn_arg.pfunc.cpf_argcount = argcount;
2026
Bram Moolenaar078a4612022-01-04 15:17:03 +00002027 // drop the arguments and the funcref/partial
2028 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002029
Bram Moolenaar078a4612022-01-04 15:17:03 +00002030 // push the return value
2031 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002032
2033 // If partial is above the arguments it must be cleared and replaced with
2034 // the return value.
2035 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2036 return FAIL;
2037
2038 return OK;
2039}
2040
2041/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002042 * Generate an ISN_DEFER instruction.
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00002043 * "obj_method" is one for "obj.Method()", zero otherwise.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002044 */
2045 int
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00002046generate_DEFER(cctx_T *cctx, int var_idx, int obj_method, int argcount)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002047{
2048 isn_T *isn;
2049
2050 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00002051 if ((isn = generate_instr_drop(cctx,
2052 obj_method == 0 ? ISN_DEFER : ISN_DEFEROBJ,
2053 argcount + 1)) == NULL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002054 return FAIL;
2055 isn->isn_arg.defer.defer_var_idx = var_idx;
2056 isn->isn_arg.defer.defer_argcount = argcount;
2057 return OK;
2058}
2059
2060/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002061 * Generate an ISN_STRINGMEMBER instruction.
2062 */
2063 int
2064generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
2065{
2066 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002067 type_T *type;
2068
2069 RETURN_OK_IF_SKIP(cctx);
2070 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
2071 return FAIL;
2072 isn->isn_arg.string = vim_strnsave(name, len);
2073
2074 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00002075 type = get_type_on_stack(cctx, 0);
Bram Moolenaar0089ce22022-10-08 14:39:36 +01002076 if (type->tt_type != VAR_DICT
2077 && type->tt_type != VAR_ANY && type->tt_type != VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002078 {
2079 char *tofree;
2080
2081 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
2082 name, type_name(type, &tofree));
2083 vim_free(tofree);
2084 return FAIL;
2085 }
2086 // change dict type to dict member type
2087 if (type->tt_type == VAR_DICT)
2088 {
Bram Moolenaar0089ce22022-10-08 14:39:36 +01002089 type_T *ntype = type->tt_member->tt_type == VAR_UNKNOWN
Bram Moolenaar078a4612022-01-04 15:17:03 +00002090 ? &t_any : type->tt_member;
2091 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002092 }
2093
2094 return OK;
2095}
2096
2097/*
2098 * Generate an ISN_ECHO instruction.
2099 */
2100 int
2101generate_ECHO(cctx_T *cctx, int with_white, int count)
2102{
2103 isn_T *isn;
2104
2105 RETURN_OK_IF_SKIP(cctx);
2106 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2107 return FAIL;
2108 isn->isn_arg.echo.echo_with_white = with_white;
2109 isn->isn_arg.echo.echo_count = count;
2110
2111 return OK;
2112}
2113
2114/*
2115 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
2116 */
2117 int
2118generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
2119{
2120 isn_T *isn;
2121
Bram Moolenaar2eae3d22022-10-07 15:09:27 +01002122 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002123 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
2124 return FAIL;
2125 isn->isn_arg.number = count;
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002126 return OK;
2127}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002128
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002129/*
2130 * Generate an ISN_ECHOWINDOW instruction
2131 */
2132 int
2133generate_ECHOWINDOW(cctx_T *cctx, int count, long time)
2134{
2135 isn_T *isn;
2136
2137 if ((isn = generate_instr_drop(cctx, ISN_ECHOWINDOW, count)) == NULL)
2138 return FAIL;
2139 isn->isn_arg.echowin.ewin_count = count;
2140 isn->isn_arg.echowin.ewin_time = time;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002141 return OK;
2142}
2143
2144/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002145 * Generate an ISN_SOURCE instruction.
2146 */
2147 int
2148generate_SOURCE(cctx_T *cctx, int sid)
2149{
2150 isn_T *isn;
2151
2152 if ((isn = generate_instr(cctx, ISN_SOURCE)) == NULL)
2153 return FAIL;
2154 isn->isn_arg.number = sid;
2155
2156 return OK;
2157}
2158
2159/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002160 * Generate an ISN_PUT instruction.
2161 */
2162 int
2163generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2164{
2165 isn_T *isn;
2166
2167 RETURN_OK_IF_SKIP(cctx);
2168 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2169 return FAIL;
2170 isn->isn_arg.put.put_regname = regname;
2171 isn->isn_arg.put.put_lnum = lnum;
2172 return OK;
2173}
2174
2175/*
2176 * Generate an EXEC instruction that takes a string argument.
2177 * A copy is made of "line".
2178 */
2179 int
2180generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
2181{
2182 isn_T *isn;
2183
2184 RETURN_OK_IF_SKIP(cctx);
2185 if ((isn = generate_instr(cctx, isntype)) == NULL)
2186 return FAIL;
2187 isn->isn_arg.string = vim_strsave(line);
2188 return OK;
2189}
2190
2191/*
2192 * Generate an EXEC instruction that takes a string argument.
2193 * "str" must be allocated, it is consumed.
2194 */
2195 int
2196generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
2197{
2198 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002199 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002200
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002201 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002202 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002203 if ((isn = generate_instr(cctx, isntype)) == NULL)
2204 ret = FAIL;
2205 else
2206 {
2207 isn->isn_arg.string = str;
2208 return OK;
2209 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002210 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002211 vim_free(str);
2212 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002213}
2214
2215 int
2216generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2217{
2218 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002219
2220 RETURN_OK_IF_SKIP(cctx);
2221 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2222 return FAIL;
2223 isn->isn_arg.string = vim_strsave(line);
2224
Bram Moolenaar078a4612022-01-04 15:17:03 +00002225 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002226}
2227
2228 int
2229generate_EXECCONCAT(cctx_T *cctx, int count)
2230{
2231 isn_T *isn;
2232
2233 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2234 return FAIL;
2235 isn->isn_arg.number = count;
2236 return OK;
2237}
2238
2239/*
2240 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2241 */
2242 int
2243generate_RANGE(cctx_T *cctx, char_u *range)
2244{
2245 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002246
2247 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2248 return FAIL;
2249 isn->isn_arg.string = range;
2250
Bram Moolenaar078a4612022-01-04 15:17:03 +00002251 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002252}
2253
2254 int
2255generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2256{
2257 isn_T *isn;
2258
2259 RETURN_OK_IF_SKIP(cctx);
2260 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2261 return FAIL;
2262 isn->isn_arg.unpack.unp_count = var_count;
2263 isn->isn_arg.unpack.unp_semicolon = semicolon;
2264 return OK;
2265}
2266
2267/*
2268 * Generate an instruction for any command modifiers.
2269 */
2270 int
2271generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
2272{
2273 isn_T *isn;
2274
2275 if (has_cmdmod(cmod, FALSE))
2276 {
2277 cctx->ctx_has_cmdmod = TRUE;
2278
2279 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
2280 return FAIL;
2281 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2282 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2283 return FAIL;
2284 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
2285 // filter program now belongs to the instruction
2286 cmod->cmod_filter_regmatch.regprog = NULL;
2287 }
2288
2289 return OK;
2290}
2291
2292 int
2293generate_undo_cmdmods(cctx_T *cctx)
2294{
2295 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2296 return FAIL;
2297 cctx->ctx_has_cmdmod = FALSE;
2298 return OK;
2299}
2300
2301/*
2302 * Generate a STORE instruction for "dest", not being "dest_local".
Bram Moolenaard505d172022-12-18 21:42:55 +00002303 * "lhs" might be NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002304 * Return FAIL when out of memory.
2305 */
2306 int
2307generate_store_var(
2308 cctx_T *cctx,
2309 assign_dest_T dest,
2310 int opt_flags,
2311 int vimvaridx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002312 type_T *type,
Bram Moolenaard505d172022-12-18 21:42:55 +00002313 char_u *name,
2314 lhs_T *lhs)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002315{
2316 switch (dest)
2317 {
2318 case dest_option:
2319 return generate_STOREOPT(cctx, ISN_STOREOPT,
2320 skip_option_env_lead(name), opt_flags);
2321 case dest_func_option:
2322 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
2323 skip_option_env_lead(name), opt_flags);
2324 case dest_global:
2325 // include g: with the name, easier to execute that way
2326 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
2327 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
2328 case dest_buffer:
2329 // include b: with the name, easier to execute that way
2330 return generate_STORE(cctx, ISN_STOREB, 0, name);
2331 case dest_window:
2332 // include w: with the name, easier to execute that way
2333 return generate_STORE(cctx, ISN_STOREW, 0, name);
2334 case dest_tab:
2335 // include t: with the name, easier to execute that way
2336 return generate_STORE(cctx, ISN_STORET, 0, name);
2337 case dest_env:
2338 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
2339 case dest_reg:
2340 return generate_STORE(cctx, ISN_STOREREG,
2341 name[1] == '@' ? '"' : name[1], NULL);
2342 case dest_vimvar:
2343 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
2344 case dest_script:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002345 {
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002346 int scriptvar_idx = lhs->lhs_scriptvar_idx;
2347 int scriptvar_sid = lhs->lhs_scriptvar_sid;
2348 if (scriptvar_idx < 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002349 {
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002350 isntype_T isn_type = ISN_STORES;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002351
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002352 if (SCRIPT_ID_VALID(scriptvar_sid)
2353 && SCRIPT_ITEM(scriptvar_sid)->sn_import_autoload
2354 && SCRIPT_ITEM(scriptvar_sid)->sn_autoload_prefix
2355 == NULL)
2356 {
2357 // "import autoload './dir/script.vim'" - load script
2358 // first
2359 if (generate_SOURCE(cctx, scriptvar_sid) == FAIL)
2360 return FAIL;
2361 isn_type = ISN_STOREEXPORT;
2362 }
2363
2364 // "s:" may be included in the name.
2365 return generate_OLDSCRIPT(cctx, isn_type, name,
2366 scriptvar_sid, type);
2367 }
2368 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002369 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002370 }
Bram Moolenaard505d172022-12-18 21:42:55 +00002371 case dest_class_member:
2372 return generate_CLASSMEMBER(cctx, FALSE,
2373 lhs->lhs_class, lhs->lhs_classmember_idx);
2374
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002375 case dest_local:
2376 case dest_expr:
2377 // cannot happen
2378 break;
2379 }
2380 return FAIL;
2381}
2382
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002383/*
2384 * Return TRUE when inside a "for" or "while" loop.
2385 */
2386 int
2387inside_loop_scope(cctx_T *cctx)
2388{
2389 scope_T *scope = cctx->ctx_scope;
2390
2391 for (;;)
2392 {
2393 if (scope == NULL)
2394 break;
2395 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
2396 return TRUE;
2397 scope = scope->se_outer;
2398 }
2399 return FALSE;
2400}
2401
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002402 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002403generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002404{
2405 if (lhs->lhs_dest != dest_local)
2406 return generate_store_var(cctx, lhs->lhs_dest,
2407 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
Bram Moolenaard505d172022-12-18 21:42:55 +00002408 lhs->lhs_type, lhs->lhs_name, lhs);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002409
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002410 if (lhs->lhs_lvar == NULL)
2411 return OK;
2412
2413 garray_T *instr = &cctx->ctx_instr;
2414 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2415
2416 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
2417 // ISN_STORENR.
2418 // And "var = 0" does not need any instruction.
2419 if (lhs->lhs_lvar->lv_from_outer == 0
2420 && instr->ga_len == instr_count + 1
2421 && isn->isn_type == ISN_PUSHNR)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002422 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002423 varnumber_T val = isn->isn_arg.number;
2424 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002425
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002426 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002427 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002428 // zero is the default value, no need to do anything
2429 --instr->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002430 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002431 else
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002432 {
2433 isn->isn_type = ISN_STORENR;
2434 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
2435 isn->isn_arg.storenr.stnr_val = val;
2436 }
2437 if (stack->ga_len > 0)
2438 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002439 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002440 else if (lhs->lhs_lvar->lv_from_outer > 0)
2441 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
2442 lhs->lhs_lvar->lv_from_outer, lhs->lhs_lvar->lv_loop_idx);
2443 else
2444 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002445 return OK;
2446}
2447
2448#if defined(FEAT_PROFILE) || defined(PROTO)
2449 void
2450may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2451{
2452 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
2453 generate_instr(cctx, ISN_PROF_END);
2454}
2455#endif
2456
2457
2458/*
2459 * Delete an instruction, free what it contains.
2460 */
2461 void
2462delete_instr(isn_T *isn)
2463{
2464 switch (isn->isn_type)
2465 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002466 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002467 case ISN_DEF:
2468 case ISN_EXEC:
2469 case ISN_EXECRANGE:
2470 case ISN_EXEC_SPLIT:
2471 case ISN_LEGACY_EVAL:
2472 case ISN_LOADAUTO:
2473 case ISN_LOADB:
2474 case ISN_LOADENV:
2475 case ISN_LOADG:
2476 case ISN_LOADOPT:
2477 case ISN_LOADT:
2478 case ISN_LOADW:
2479 case ISN_LOCKUNLOCK:
2480 case ISN_PUSHEXC:
2481 case ISN_PUSHFUNC:
2482 case ISN_PUSHS:
2483 case ISN_RANGE:
2484 case ISN_STOREAUTO:
2485 case ISN_STOREB:
2486 case ISN_STOREENV:
2487 case ISN_STOREG:
2488 case ISN_STORET:
2489 case ISN_STOREW:
2490 case ISN_STRINGMEMBER:
2491 vim_free(isn->isn_arg.string);
2492 break;
2493
2494 case ISN_SUBSTITUTE:
2495 {
2496 int idx;
2497 isn_T *list = isn->isn_arg.subs.subs_instr;
2498
2499 vim_free(isn->isn_arg.subs.subs_cmd);
2500 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2501 delete_instr(list + idx);
2502 vim_free(list);
2503 }
2504 break;
2505
2506 case ISN_INSTR:
2507 {
2508 int idx;
2509 isn_T *list = isn->isn_arg.instr;
2510
2511 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2512 delete_instr(list + idx);
2513 vim_free(list);
2514 }
2515 break;
2516
2517 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002518 case ISN_LOADEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002519 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002520 case ISN_STOREEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002521 vim_free(isn->isn_arg.loadstore.ls_name);
2522 break;
2523
2524 case ISN_UNLET:
2525 case ISN_UNLETENV:
2526 vim_free(isn->isn_arg.unlet.ul_name);
2527 break;
2528
2529 case ISN_STOREOPT:
2530 case ISN_STOREFUNCOPT:
2531 vim_free(isn->isn_arg.storeopt.so_name);
2532 break;
2533
2534 case ISN_PUSHBLOB: // push blob isn_arg.blob
2535 blob_unref(isn->isn_arg.blob);
2536 break;
2537
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002538 case ISN_PUSHCLASS:
Bram Moolenaar30a84472023-02-27 08:07:14 +00002539 class_unref(isn->isn_arg.classarg);
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002540 break;
2541
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002542 case ISN_UCALL:
2543 vim_free(isn->isn_arg.ufunc.cuf_name);
2544 break;
2545
2546 case ISN_FUNCREF:
2547 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002548 funcref_T *funcref = &isn->isn_arg.funcref;
2549 funcref_extra_T *extra = funcref->fr_extra;
2550
2551 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002552 {
2553 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002554 + funcref->fr_dfunc_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002555 ufunc_T *ufunc = dfunc->df_ufunc;
2556
2557 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2558 func_ptr_unref(ufunc);
2559 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002560 if (extra != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002561 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002562 char_u *name = extra->fre_func_name;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002563
2564 if (name != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002565 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002566 func_unref(name);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002567 vim_free(name);
2568 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00002569 if (extra->fre_class != NULL)
2570 class_unref(extra->fre_class);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002571 vim_free(extra);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002572 }
2573 }
2574 break;
2575
2576 case ISN_DCALL:
2577 {
2578 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002579 + isn->isn_arg.dfunc.cdf_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002580
2581 if (dfunc->df_ufunc != NULL
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002582 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002583 func_ptr_unref(dfunc->df_ufunc);
2584 }
2585 break;
2586
Bram Moolenaard0200c82023-01-28 15:19:40 +00002587 case ISN_METHODCALL:
2588 {
2589 cmfunc_T *mfunc = isn->isn_arg.mfunc;
2590 class_unref(mfunc->cmf_itf);
2591 vim_free(mfunc);
2592 }
2593 break;
2594
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002595 case ISN_NEWFUNC:
2596 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002597 newfuncarg_T *arg = isn->isn_arg.newfunc.nf_arg;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002598
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002599 if (arg != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002600 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002601 ufunc_T *ufunc = find_func_even_dead(
2602 arg->nfa_lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002603
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002604 if (ufunc != NULL)
2605 {
2606 unlink_def_function(ufunc);
2607 func_ptr_unref(ufunc);
2608 }
2609
2610 vim_free(arg->nfa_lambda);
2611 vim_free(arg->nfa_global);
2612 vim_free(arg);
2613 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002614 }
2615 break;
2616
2617 case ISN_CHECKTYPE:
2618 case ISN_SETTYPE:
2619 free_type(isn->isn_arg.type.ct_type);
2620 break;
2621
2622 case ISN_CMDMOD:
2623 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002624 ->cmod_filter_regmatch.regprog);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002625 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2626 break;
2627
2628 case ISN_LOADSCRIPT:
2629 case ISN_STORESCRIPT:
2630 vim_free(isn->isn_arg.script.scriptref);
2631 break;
2632
Bram Moolenaard505d172022-12-18 21:42:55 +00002633 case ISN_LOAD_CLASSMEMBER:
2634 case ISN_STORE_CLASSMEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00002635 case ISN_GET_ITF_MEMBER:
Bram Moolenaard505d172022-12-18 21:42:55 +00002636 class_unref(isn->isn_arg.classmember.cm_class);
2637 break;
2638
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002639 case ISN_STOREINDEX:
2640 class_unref(isn->isn_arg.storeindex.si_class);
2641 break;
2642
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002643 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002644 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002645 break;
2646
2647 case ISN_CEXPR_CORE:
2648 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2649 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2650 break;
2651
2652 case ISN_2BOOL:
2653 case ISN_2STRING:
2654 case ISN_2STRING_ANY:
2655 case ISN_ADDBLOB:
2656 case ISN_ADDLIST:
2657 case ISN_ANYINDEX:
2658 case ISN_ANYSLICE:
2659 case ISN_BCALL:
2660 case ISN_BLOBAPPEND:
2661 case ISN_BLOBINDEX:
2662 case ISN_BLOBSLICE:
2663 case ISN_CATCH:
2664 case ISN_CEXPR_AUCMD:
2665 case ISN_CHECKLEN:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002666 case ISN_CLEARDICT:
2667 case ISN_CMDMOD_REV:
2668 case ISN_COMPAREANY:
2669 case ISN_COMPAREBLOB:
2670 case ISN_COMPAREBOOL:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002671 case ISN_COMPARECLASS:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002672 case ISN_COMPAREDICT:
2673 case ISN_COMPAREFLOAT:
2674 case ISN_COMPAREFUNC:
2675 case ISN_COMPARELIST:
2676 case ISN_COMPARENR:
Bram Moolenaar7a222242022-03-01 19:23:24 +00002677 case ISN_COMPARENULL:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002678 case ISN_COMPAREOBJECT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002679 case ISN_COMPARESPECIAL:
2680 case ISN_COMPARESTRING:
2681 case ISN_CONCAT:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002682 case ISN_CONSTRUCT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002683 case ISN_COND2BOOL:
2684 case ISN_DEBUG:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002685 case ISN_DEFER:
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00002686 case ISN_DEFEROBJ:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002687 case ISN_DROP:
2688 case ISN_ECHO:
2689 case ISN_ECHOCONSOLE:
2690 case ISN_ECHOERR:
2691 case ISN_ECHOMSG:
Bram Moolenaar68a635a2022-09-01 17:26:17 +01002692 case ISN_ECHOWINDOW:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002693 case ISN_ENDLOOP:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002694 case ISN_ENDTRY:
2695 case ISN_EXECCONCAT:
2696 case ISN_EXECUTE:
2697 case ISN_FINALLY:
2698 case ISN_FINISH:
2699 case ISN_FOR:
2700 case ISN_GETITEM:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002701 case ISN_GET_OBJ_MEMBER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002702 case ISN_JUMP:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002703 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002704 case ISN_JUMP_IF_ARG_SET:
2705 case ISN_LISTAPPEND:
2706 case ISN_LISTINDEX:
2707 case ISN_LISTSLICE:
2708 case ISN_LOAD:
2709 case ISN_LOADBDICT:
2710 case ISN_LOADGDICT:
2711 case ISN_LOADOUTER:
2712 case ISN_LOADREG:
2713 case ISN_LOADTDICT:
2714 case ISN_LOADV:
2715 case ISN_LOADWDICT:
2716 case ISN_LOCKCONST:
2717 case ISN_MEMBER:
2718 case ISN_NEGATENR:
2719 case ISN_NEWDICT:
2720 case ISN_NEWLIST:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002721 case ISN_NEWPARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002722 case ISN_OPANY:
2723 case ISN_OPFLOAT:
2724 case ISN_OPNR:
2725 case ISN_PCALL:
2726 case ISN_PCALL_END:
2727 case ISN_PROF_END:
2728 case ISN_PROF_START:
2729 case ISN_PUSHBOOL:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002730 case ISN_PUSHCHANNEL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002731 case ISN_PUSHF:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002732 case ISN_PUSHJOB:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002733 case ISN_PUSHNR:
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002734 case ISN_PUSHOBJ:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002735 case ISN_PUSHSPEC:
2736 case ISN_PUT:
2737 case ISN_REDIREND:
2738 case ISN_REDIRSTART:
2739 case ISN_RETURN:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002740 case ISN_RETURN_OBJECT:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002741 case ISN_RETURN_VOID:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002742 case ISN_SHUFFLE:
2743 case ISN_SLICE:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002744 case ISN_SOURCE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002745 case ISN_STORE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002746 case ISN_STORENR:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002747 case ISN_STOREOUTER:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002748 case ISN_STORE_THIS:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002749 case ISN_STORERANGE:
2750 case ISN_STOREREG:
2751 case ISN_STOREV:
2752 case ISN_STRINDEX:
2753 case ISN_STRSLICE:
2754 case ISN_THROW:
2755 case ISN_TRYCONT:
2756 case ISN_UNLETINDEX:
2757 case ISN_UNLETRANGE:
2758 case ISN_UNPACK:
2759 case ISN_USEDICT:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002760 case ISN_WHILE:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002761 // nothing allocated
2762 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002763 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002764}
2765
2766 void
2767clear_instr_ga(garray_T *gap)
2768{
2769 int idx;
2770
2771 for (idx = 0; idx < gap->ga_len; ++idx)
2772 delete_instr(((isn_T *)gap->ga_data) + idx);
2773 ga_clear(gap);
2774}
2775
2776
2777#endif // defined(FEAT_EVAL)