blob: dcbe897cdaa5bf660f6c9de4e671a7f04a98d005 [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.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001381 * "isnp" is set to the instruction, so that fr_dfunc_idx can be set later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001382 */
1383 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001384generate_FUNCREF(
1385 cctx_T *cctx,
1386 ufunc_T *ufunc,
Bram Moolenaar313e4722023-02-08 20:55:27 +00001387 class_T *cl,
1388 int fi,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001389 isn_T **isnp)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001390{
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001391 isn_T *isn;
1392 type_T *type;
1393 funcref_extra_T *extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001394 loopvarinfo_T loopinfo;
1395 int has_vars;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001396
1397 RETURN_OK_IF_SKIP(cctx);
1398 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1399 return FAIL;
Bram Moolenaara915fa02022-03-23 11:29:15 +00001400 if (isnp != NULL)
1401 *isnp = isn;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001402
Bram Moolenaarcc341812022-09-19 15:54:34 +01001403 has_vars = get_loop_var_info(cctx, &loopinfo);
Bram Moolenaar313e4722023-02-08 20:55:27 +00001404 if (ufunc->uf_def_status == UF_NOT_COMPILED || has_vars || cl != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001405 {
1406 extra = ALLOC_CLEAR_ONE(funcref_extra_T);
1407 if (extra == NULL)
1408 return FAIL;
1409 isn->isn_arg.funcref.fr_extra = extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001410 extra->fre_loopvar_info = loopinfo;
Bram Moolenaar313e4722023-02-08 20:55:27 +00001411 if (cl != NULL)
1412 {
1413 extra->fre_class = cl;
1414 ++cl->class_refcount;
1415 extra->fre_method_idx = fi;
1416 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001417 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00001418 if (ufunc->uf_def_status == UF_NOT_COMPILED || cl != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001419 extra->fre_func_name = vim_strsave(ufunc->uf_name);
Bram Moolenaar313e4722023-02-08 20:55:27 +00001420 if (ufunc->uf_def_status != UF_NOT_COMPILED && cl == NULL)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001421 {
1422 if (isnp == NULL && ufunc->uf_def_status == UF_TO_BE_COMPILED)
1423 // compile the function now, we need the uf_dfunc_idx value
1424 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001425 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001426 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01001427
1428 // Reserve an extra variable to keep track of the number of closures
1429 // created.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001430 cctx->ctx_has_closure = 1;
1431
1432 // If the referenced function is a closure, it may use items further up in
Bram Moolenaar139575d2022-03-15 19:29:30 +00001433 // the nested context, including this one. But not a function defined at
1434 // the script level.
1435 if ((ufunc->uf_flags & FC_CLOSURE)
1436 && func_name_refcount(cctx->ctx_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001437 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1438
Bram Moolenaar078a4612022-01-04 15:17:03 +00001439 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1440 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001441}
1442
1443/*
1444 * Generate an ISN_NEWFUNC instruction.
1445 * "lambda_name" and "func_name" must be in allocated memory and will be
1446 * consumed.
1447 */
1448 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001449generate_NEWFUNC(
1450 cctx_T *cctx,
1451 char_u *lambda_name,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001452 char_u *func_name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001453{
1454 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001455 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001456
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001457 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001458 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001459 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1460 ret = FAIL;
1461 else
1462 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001463 newfuncarg_T *arg = ALLOC_CLEAR_ONE(newfuncarg_T);
1464
1465 if (arg == NULL)
1466 ret = FAIL;
1467 else
1468 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01001469 // Reserve an extra variable to keep track of the number of
1470 // closures created.
1471 cctx->ctx_has_closure = 1;
1472
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001473 isn->isn_arg.newfunc.nf_arg = arg;
1474 arg->nfa_lambda = lambda_name;
1475 arg->nfa_global = func_name;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001476 (void)get_loop_var_info(cctx, &arg->nfa_loopvar_info);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001477 return OK;
1478 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001479 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001480 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001481 vim_free(lambda_name);
1482 vim_free(func_name);
1483 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001484}
1485
1486/*
1487 * Generate an ISN_DEF instruction: list functions
1488 */
1489 int
1490generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1491{
1492 isn_T *isn;
1493
1494 RETURN_OK_IF_SKIP(cctx);
1495 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1496 return FAIL;
1497 if (len > 0)
1498 {
1499 isn->isn_arg.string = vim_strnsave(name, len);
1500 if (isn->isn_arg.string == NULL)
1501 return FAIL;
1502 }
1503 return OK;
1504}
1505
1506/*
1507 * Generate an ISN_JUMP instruction.
1508 */
1509 int
1510generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1511{
1512 isn_T *isn;
1513 garray_T *stack = &cctx->ctx_type_stack;
1514
1515 RETURN_OK_IF_SKIP(cctx);
1516 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1517 return FAIL;
1518 isn->isn_arg.jump.jump_when = when;
1519 isn->isn_arg.jump.jump_where = where;
1520
1521 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1522 --stack->ga_len;
1523
1524 return OK;
1525}
1526
1527/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001528 * Generate an ISN_WHILE instruction. Similar to ISN_JUMP for :while
1529 */
1530 int
1531generate_WHILE(cctx_T *cctx, int funcref_idx)
1532{
1533 isn_T *isn;
1534 garray_T *stack = &cctx->ctx_type_stack;
1535
1536 RETURN_OK_IF_SKIP(cctx);
1537 if ((isn = generate_instr(cctx, ISN_WHILE)) == NULL)
1538 return FAIL;
1539 isn->isn_arg.whileloop.while_funcref_idx = funcref_idx;
1540 isn->isn_arg.whileloop.while_end = 0; // filled in later
1541
1542 if (stack->ga_len > 0)
1543 --stack->ga_len;
1544
1545 return OK;
1546}
1547
1548/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001549 * Generate an ISN_JUMP_IF_ARG_SET or ISN_JUMP_IF_ARG_NOT_SET instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001550 */
1551 int
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001552generate_JUMP_IF_ARG(cctx_T *cctx, isntype_T isn_type, int arg_off)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001553{
1554 isn_T *isn;
1555
1556 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001557 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001558 return FAIL;
1559 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1560 // jump_where is set later
1561 return OK;
1562}
1563
1564 int
1565generate_FOR(cctx_T *cctx, int loop_idx)
1566{
1567 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001568
1569 RETURN_OK_IF_SKIP(cctx);
1570 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1571 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001572 isn->isn_arg.forloop.for_loop_idx = loop_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001573
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001574 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001575 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001576}
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001577
1578 int
Bram Moolenaarcc341812022-09-19 15:54:34 +01001579generate_ENDLOOP(cctx_T *cctx, loop_info_T *loop_info)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001580{
1581 isn_T *isn;
1582
1583 RETURN_OK_IF_SKIP(cctx);
1584 if ((isn = generate_instr(cctx, ISN_ENDLOOP)) == NULL)
1585 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001586 isn->isn_arg.endloop.end_depth = loop_info->li_depth;
1587 isn->isn_arg.endloop.end_funcref_idx = loop_info->li_funcref_idx;
1588 isn->isn_arg.endloop.end_var_idx = loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001589 isn->isn_arg.endloop.end_var_count =
Bram Moolenaarcc341812022-09-19 15:54:34 +01001590 cctx->ctx_locals.ga_len - loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001591 return OK;
1592}
1593
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001594/*
1595 * Generate an ISN_TRYCONT instruction.
1596 */
1597 int
1598generate_TRYCONT(cctx_T *cctx, int levels, int where)
1599{
1600 isn_T *isn;
1601
1602 RETURN_OK_IF_SKIP(cctx);
1603 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1604 return FAIL;
1605 isn->isn_arg.trycont.tct_levels = levels;
1606 isn->isn_arg.trycont.tct_where = where;
1607
1608 return OK;
1609}
1610
Bram Moolenaar16900322022-09-08 19:51:45 +01001611/*
1612 * Check "argount" arguments and their types on the type stack.
1613 * Give an error and return FAIL if something is wrong.
1614 * When "method_call" is NULL no code is generated.
1615 */
1616 int
1617check_internal_func_args(
1618 cctx_T *cctx,
1619 int func_idx,
1620 int argcount,
1621 int method_call,
1622 type2_T **argtypes,
1623 type2_T *shuffled_argtypes)
1624{
1625 garray_T *stack = &cctx->ctx_type_stack;
1626 int argoff = check_internal_func(func_idx, argcount);
1627
1628 if (argoff < 0)
1629 return FAIL;
1630
1631 if (method_call && argoff > 1)
1632 {
Bram Moolenaarb7f22702023-04-27 16:24:07 +01001633 if (argcount < argoff)
1634 {
1635 semsg(_(e_not_enough_arguments_for_function_str),
1636 internal_func_name(func_idx));
1637 return FAIL;
1638 }
Bram Moolenaar16900322022-09-08 19:51:45 +01001639
Bram Moolenaarb7f22702023-04-27 16:24:07 +01001640 isn_T *isn = generate_instr(cctx, ISN_SHUFFLE);
Bram Moolenaar16900322022-09-08 19:51:45 +01001641 if (isn == NULL)
1642 return FAIL;
1643 isn->isn_arg.shuffle.shfl_item = argcount;
1644 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1645 }
1646
1647 if (argcount > 0)
1648 {
1649 type2_T *typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
1650
1651 // Check the types of the arguments.
1652 if (method_call && argoff > 1)
1653 {
1654 int i;
1655
1656 for (i = 0; i < argcount; ++i)
1657 shuffled_argtypes[i] = (i < argoff - 1)
1658 ? typep[i + 1]
1659 : (i == argoff - 1) ? typep[0] : typep[i];
1660 *argtypes = shuffled_argtypes;
1661 }
1662 else
1663 {
1664 int i;
1665
1666 for (i = 0; i < argcount; ++i)
1667 shuffled_argtypes[i] = typep[i];
1668 *argtypes = shuffled_argtypes;
1669 }
1670 if (internal_func_check_arg_types(*argtypes, func_idx, argcount,
1671 cctx) == FAIL)
1672 return FAIL;
1673 }
1674 return OK;
1675}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001676
1677/*
1678 * Generate an ISN_BCALL instruction.
1679 * "method_call" is TRUE for "value->method()"
1680 * Return FAIL if the number of arguments is wrong.
1681 */
1682 int
1683generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1684{
1685 isn_T *isn;
1686 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001687 type2_T *argtypes = NULL;
1688 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1689 type2_T *maptype = NULL;
1690 type_T *type;
Bram Moolenaar81330182022-02-01 12:11:58 +00001691 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001692
1693 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar16900322022-09-08 19:51:45 +01001694
1695 if (check_internal_func_args(cctx, func_idx, argcount, method_call,
1696 &argtypes, shuffled_argtypes) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001697 return FAIL;
1698
Bram Moolenaar16900322022-09-08 19:51:45 +01001699 if (internal_func_is_map(func_idx))
1700 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001701
1702 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1703 return FAIL;
1704 isn->isn_arg.bfunc.cbf_idx = func_idx;
1705 isn->isn_arg.bfunc.cbf_argcount = argcount;
1706
1707 // Drop the argument types and push the return type.
1708 stack->ga_len -= argcount;
Bram Moolenaar32517c42023-01-15 18:17:12 +00001709 type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type,
1710 cctx->ctx_type_list);
Bram Moolenaar81330182022-02-01 12:11:58 +00001711 if (push_type_stack2(cctx, type, decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001712 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001713
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001714 if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
1715 && maptype[0].type_decl->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001716 // Check that map() didn't change the item types.
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001717 generate_TYPECHECK(cctx, maptype[0].type_decl, FALSE, -1, FALSE, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001718
1719 return OK;
1720}
1721
1722/*
1723 * Generate an ISN_LISTAPPEND instruction. Works like add().
1724 * Argument count is already checked.
1725 */
1726 int
1727generate_LISTAPPEND(cctx_T *cctx)
1728{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001729 type_T *list_type;
1730 type_T *item_type;
1731 type_T *expected;
1732
1733 // Caller already checked that list_type is a list.
Bram Moolenaar81330182022-02-01 12:11:58 +00001734 // For checking the item type we use the declared type of the list and the
1735 // current type of the added item, adding a string to [1, 2] is OK.
1736 list_type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaarfa103972022-09-29 19:14:42 +01001737 if (arg_type_modifiable(list_type, 1) == FAIL)
1738 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001739 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001740 expected = list_type->tt_member;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001741 if (need_type(item_type, expected, FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001742 return FAIL;
1743
1744 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1745 return FAIL;
1746
Bram Moolenaar078a4612022-01-04 15:17:03 +00001747 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001748 return OK;
1749}
1750
1751/*
1752 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1753 * Argument count is already checked.
1754 */
1755 int
1756generate_BLOBAPPEND(cctx_T *cctx)
1757{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001758 type_T *item_type;
1759
Bram Moolenaarfa103972022-09-29 19:14:42 +01001760 // Caller already checked that blob_type is a blob, check it is modifiable.
1761 if (arg_type_modifiable(get_decl_type_on_stack(cctx, 1), 1) == FAIL)
1762 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001763 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001764 if (need_type(item_type, &t_number, FALSE,
1765 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001766 return FAIL;
1767
1768 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1769 return FAIL;
1770
Bram Moolenaar078a4612022-01-04 15:17:03 +00001771 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001772 return OK;
1773}
1774
1775/*
Bram Moolenaard0200c82023-01-28 15:19:40 +00001776 * Generate an ISN_DCALL, ISN_UCALL or ISN_METHODCALL instruction.
1777 * When calling a method on an object, of which we know the interface only,
1778 * then "cl" is the interface and "mi" the method index on the interface.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001779 * Return FAIL if the number of arguments is wrong.
1780 */
1781 int
Bram Moolenaard0200c82023-01-28 15:19:40 +00001782generate_CALL(
1783 cctx_T *cctx,
1784 ufunc_T *ufunc,
1785 class_T *cl,
1786 int mi,
h-east2261c892023-08-16 21:49:54 +09001787 type_T *mtype, // method type
Bram Moolenaard0200c82023-01-28 15:19:40 +00001788 int pushed_argcount)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001789{
1790 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001791 int regular_args = ufunc->uf_args.ga_len;
1792 int argcount = pushed_argcount;
1793
1794 RETURN_OK_IF_SKIP(cctx);
1795 if (argcount > regular_args && !has_varargs(ufunc))
1796 {
1797 semsg(_(e_too_many_arguments_for_function_str),
1798 printable_func_name(ufunc));
1799 return FAIL;
1800 }
1801 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1802 {
1803 semsg(_(e_not_enough_arguments_for_function_str),
1804 printable_func_name(ufunc));
1805 return FAIL;
1806 }
1807
1808 if (ufunc->uf_def_status != UF_NOT_COMPILED
1809 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1810 {
1811 int i;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001812 compiletype_T compile_type;
h-east2261c892023-08-16 21:49:54 +09001813 int class_constructor = (mtype->tt_type == VAR_CLASS
1814 && STRNCMP(ufunc->uf_name, "new", 3) == 0);
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];
h-east2261c892023-08-16 21:49:54 +09001833
1834 // When the method is a class constructor and the formal
1835 // argument is an object member, the type check is performed on
1836 // the object member type.
1837 if (class_constructor && expected->tt_type == VAR_ANY)
1838 {
1839 class_T *clp = mtype->tt_class;
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02001840 char_u *aname = ((char_u **)ufunc->uf_args.ga_data)[i];
1841 int m_idx;
1842 ocmember_T *m = object_member_lookup(clp, aname, 0,
1843 &m_idx);
1844 if (m != NULL)
1845 expected = m->ocm_type;
h-east2261c892023-08-16 21:49:54 +09001846 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001847 }
1848 else if (ufunc->uf_va_type == NULL
1849 || ufunc->uf_va_type == &t_list_any)
1850 // possibly a lambda or "...: any"
1851 expected = &t_any;
1852 else
1853 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001854 if (need_type(actual, expected, FALSE,
1855 -argcount + i, i + 1, cctx, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001856 {
1857 arg_type_mismatch(expected, actual, i + 1);
1858 return FAIL;
1859 }
1860 }
Bram Moolenaar139575d2022-03-15 19:29:30 +00001861 compile_type = get_compile_type(ufunc);
1862 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001863 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaar139575d2022-03-15 19:29:30 +00001864 compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001865 return FAIL;
1866 }
1867 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1868 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001869 emsg_funcname(e_call_to_function_that_failed_to_compile_str,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001870 ufunc->uf_name);
1871 return FAIL;
1872 }
1873
Bram Moolenaard0200c82023-01-28 15:19:40 +00001874 if ((isn = generate_instr(cctx, cl != NULL ? ISN_METHODCALL
1875 : ufunc->uf_def_status != UF_NOT_COMPILED
1876 ? ISN_DCALL : ISN_UCALL)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001877 return FAIL;
Bram Moolenaar836137d2023-01-29 14:11:24 +00001878 if (cl != NULL /* isn->isn_type == ISN_METHODCALL */)
Bram Moolenaard0200c82023-01-28 15:19:40 +00001879 {
1880 isn->isn_arg.mfunc = ALLOC_ONE(cmfunc_T);
1881 if (isn->isn_arg.mfunc == NULL)
1882 return FAIL;
1883 isn->isn_arg.mfunc->cmf_itf = cl;
1884 ++cl->class_refcount;
1885 isn->isn_arg.mfunc->cmf_idx = mi;
1886 isn->isn_arg.mfunc->cmf_argcount = argcount;
1887 }
1888 else if (isn->isn_type == ISN_DCALL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001889 {
1890 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1891 isn->isn_arg.dfunc.cdf_argcount = argcount;
1892 }
1893 else
1894 {
1895 // A user function may be deleted and redefined later, can't use the
1896 // ufunc pointer, need to look it up again at runtime.
1897 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1898 isn->isn_arg.ufunc.cuf_argcount = argcount;
1899 }
1900
Bram Moolenaar078a4612022-01-04 15:17:03 +00001901 // drop the argument types
1902 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001903
Yegappan Lakshmanan639751d2023-08-27 19:23:37 +02001904 // For an object or class method call, drop the object/class type
1905 if (ufunc->uf_class != NULL)
1906 cctx->ctx_type_stack.ga_len--;
1907
Bram Moolenaar078a4612022-01-04 15:17:03 +00001908 // add return type
1909 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001910}
1911
1912/*
1913 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1914 */
1915 int
1916generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1917{
1918 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001919
1920 RETURN_OK_IF_SKIP(cctx);
1921 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1922 return FAIL;
1923 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1924 isn->isn_arg.ufunc.cuf_argcount = argcount;
1925
Bram Moolenaar078a4612022-01-04 15:17:03 +00001926 // drop the argument types
1927 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001928
Bram Moolenaar078a4612022-01-04 15:17:03 +00001929 // add return value
1930 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001931}
1932
1933/*
Bram Moolenaar16900322022-09-08 19:51:45 +01001934 * Check the arguments of function "type" against the types on the stack.
1935 * Returns OK or FAIL;
1936 */
1937 int
1938check_func_args_from_type(
1939 cctx_T *cctx,
1940 type_T *type,
1941 int argcount,
1942 int at_top,
1943 char_u *name)
1944{
1945 if (type->tt_argcount != -1)
1946 {
1947 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1948
1949 if (argcount < type->tt_min_argcount - varargs)
1950 {
1951 emsg_funcname(e_not_enough_arguments_for_function_str, name);
1952 return FAIL;
1953 }
1954 if (!varargs && argcount > type->tt_argcount)
1955 {
1956 emsg_funcname(e_too_many_arguments_for_function_str, name);
1957 return FAIL;
1958 }
1959 if (type->tt_args != NULL)
1960 {
1961 int i;
1962
1963 for (i = 0; i < argcount; ++i)
1964 {
1965 int offset = -argcount + i - (at_top ? 0 : 1);
1966 type_T *actual = get_type_on_stack(cctx, -1 - offset);
1967 type_T *expected;
1968
1969 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaar36818a92023-01-03 12:33:26 +00001970 {
1971 expected = type->tt_args[type->tt_argcount - 1];
1972 if (expected != NULL && expected->tt_type == VAR_LIST)
1973 expected = expected->tt_member;
1974 if (expected == NULL)
1975 expected = &t_any;
1976 }
Bram Moolenaar16900322022-09-08 19:51:45 +01001977 else if (i >= type->tt_min_argcount
1978 && actual->tt_type == VAR_SPECIAL)
1979 expected = &t_any;
1980 else
1981 expected = type->tt_args[i];
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001982 if (need_type(actual, expected, FALSE,
1983 offset, i + 1, cctx, TRUE, FALSE) == FAIL)
Bram Moolenaar16900322022-09-08 19:51:45 +01001984 {
1985 arg_type_mismatch(expected, actual, i + 1);
1986 return FAIL;
1987 }
1988 }
1989 }
1990 }
1991
1992 return OK;
1993}
1994/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001995 * Generate an ISN_PCALL instruction.
1996 * "type" is the type of the FuncRef.
1997 */
1998 int
1999generate_PCALL(
2000 cctx_T *cctx,
2001 int argcount,
2002 char_u *name,
2003 type_T *type,
2004 int at_top)
2005{
2006 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002007 type_T *ret_type;
2008
2009 RETURN_OK_IF_SKIP(cctx);
2010
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002011 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002012 ret_type = &t_any;
2013 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
2014 {
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002015 if (check_func_args_from_type(cctx, type, argcount, at_top, name)
2016 == FAIL)
Bram Moolenaar16900322022-09-08 19:51:45 +01002017 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002018
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002019 ret_type = type->tt_member;
2020 if (ret_type == &t_unknown)
2021 // return type not known yet, use a runtime check
2022 ret_type = &t_any;
2023 }
2024 else
2025 {
2026 semsg(_(e_not_callable_type_str), name);
2027 return FAIL;
2028 }
2029
2030 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2031 return FAIL;
2032 isn->isn_arg.pfunc.cpf_top = at_top;
2033 isn->isn_arg.pfunc.cpf_argcount = argcount;
2034
Bram Moolenaar078a4612022-01-04 15:17:03 +00002035 // drop the arguments and the funcref/partial
2036 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002037
Bram Moolenaar078a4612022-01-04 15:17:03 +00002038 // push the return value
2039 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002040
2041 // If partial is above the arguments it must be cleared and replaced with
2042 // the return value.
2043 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2044 return FAIL;
2045
2046 return OK;
2047}
2048
2049/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002050 * Generate an ISN_DEFER instruction.
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00002051 * "obj_method" is one for "obj.Method()", zero otherwise.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002052 */
2053 int
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00002054generate_DEFER(cctx_T *cctx, int var_idx, int obj_method, int argcount)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002055{
2056 isn_T *isn;
2057
2058 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00002059 if ((isn = generate_instr_drop(cctx,
2060 obj_method == 0 ? ISN_DEFER : ISN_DEFEROBJ,
2061 argcount + 1)) == NULL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002062 return FAIL;
2063 isn->isn_arg.defer.defer_var_idx = var_idx;
2064 isn->isn_arg.defer.defer_argcount = argcount;
2065 return OK;
2066}
2067
2068/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002069 * Generate an ISN_STRINGMEMBER instruction.
2070 */
2071 int
2072generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
2073{
2074 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002075 type_T *type;
2076
2077 RETURN_OK_IF_SKIP(cctx);
2078 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
2079 return FAIL;
2080 isn->isn_arg.string = vim_strnsave(name, len);
2081
2082 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00002083 type = get_type_on_stack(cctx, 0);
Bram Moolenaar0089ce22022-10-08 14:39:36 +01002084 if (type->tt_type != VAR_DICT
2085 && type->tt_type != VAR_ANY && type->tt_type != VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002086 {
2087 char *tofree;
2088
2089 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
2090 name, type_name(type, &tofree));
2091 vim_free(tofree);
2092 return FAIL;
2093 }
2094 // change dict type to dict member type
2095 if (type->tt_type == VAR_DICT)
2096 {
Bram Moolenaar0089ce22022-10-08 14:39:36 +01002097 type_T *ntype = type->tt_member->tt_type == VAR_UNKNOWN
Bram Moolenaar078a4612022-01-04 15:17:03 +00002098 ? &t_any : type->tt_member;
2099 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002100 }
2101
2102 return OK;
2103}
2104
2105/*
2106 * Generate an ISN_ECHO instruction.
2107 */
2108 int
2109generate_ECHO(cctx_T *cctx, int with_white, int count)
2110{
2111 isn_T *isn;
2112
2113 RETURN_OK_IF_SKIP(cctx);
2114 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2115 return FAIL;
2116 isn->isn_arg.echo.echo_with_white = with_white;
2117 isn->isn_arg.echo.echo_count = count;
2118
2119 return OK;
2120}
2121
2122/*
2123 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
2124 */
2125 int
2126generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
2127{
2128 isn_T *isn;
2129
Bram Moolenaar2eae3d22022-10-07 15:09:27 +01002130 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002131 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
2132 return FAIL;
2133 isn->isn_arg.number = count;
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002134 return OK;
2135}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002136
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002137/*
2138 * Generate an ISN_ECHOWINDOW instruction
2139 */
2140 int
2141generate_ECHOWINDOW(cctx_T *cctx, int count, long time)
2142{
2143 isn_T *isn;
2144
2145 if ((isn = generate_instr_drop(cctx, ISN_ECHOWINDOW, count)) == NULL)
2146 return FAIL;
2147 isn->isn_arg.echowin.ewin_count = count;
2148 isn->isn_arg.echowin.ewin_time = time;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002149 return OK;
2150}
2151
2152/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002153 * Generate an ISN_SOURCE instruction.
2154 */
2155 int
2156generate_SOURCE(cctx_T *cctx, int sid)
2157{
2158 isn_T *isn;
2159
2160 if ((isn = generate_instr(cctx, ISN_SOURCE)) == NULL)
2161 return FAIL;
2162 isn->isn_arg.number = sid;
2163
2164 return OK;
2165}
2166
2167/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002168 * Generate an ISN_PUT instruction.
2169 */
2170 int
2171generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2172{
2173 isn_T *isn;
2174
2175 RETURN_OK_IF_SKIP(cctx);
2176 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2177 return FAIL;
2178 isn->isn_arg.put.put_regname = regname;
2179 isn->isn_arg.put.put_lnum = lnum;
2180 return OK;
2181}
2182
2183/*
2184 * Generate an EXEC instruction that takes a string argument.
2185 * A copy is made of "line".
2186 */
2187 int
2188generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
2189{
2190 isn_T *isn;
2191
2192 RETURN_OK_IF_SKIP(cctx);
2193 if ((isn = generate_instr(cctx, isntype)) == NULL)
2194 return FAIL;
2195 isn->isn_arg.string = vim_strsave(line);
2196 return OK;
2197}
2198
2199/*
2200 * Generate an EXEC instruction that takes a string argument.
2201 * "str" must be allocated, it is consumed.
2202 */
2203 int
2204generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
2205{
2206 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002207 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002208
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002209 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002210 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002211 if ((isn = generate_instr(cctx, isntype)) == NULL)
2212 ret = FAIL;
2213 else
2214 {
2215 isn->isn_arg.string = str;
2216 return OK;
2217 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002218 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002219 vim_free(str);
2220 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002221}
2222
2223 int
2224generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2225{
2226 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002227
2228 RETURN_OK_IF_SKIP(cctx);
2229 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2230 return FAIL;
2231 isn->isn_arg.string = vim_strsave(line);
2232
Bram Moolenaar078a4612022-01-04 15:17:03 +00002233 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002234}
2235
2236 int
2237generate_EXECCONCAT(cctx_T *cctx, int count)
2238{
2239 isn_T *isn;
2240
2241 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2242 return FAIL;
2243 isn->isn_arg.number = count;
2244 return OK;
2245}
2246
2247/*
2248 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2249 */
2250 int
2251generate_RANGE(cctx_T *cctx, char_u *range)
2252{
2253 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002254
2255 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2256 return FAIL;
2257 isn->isn_arg.string = range;
2258
Bram Moolenaar078a4612022-01-04 15:17:03 +00002259 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002260}
2261
2262 int
2263generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2264{
2265 isn_T *isn;
2266
2267 RETURN_OK_IF_SKIP(cctx);
2268 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2269 return FAIL;
2270 isn->isn_arg.unpack.unp_count = var_count;
2271 isn->isn_arg.unpack.unp_semicolon = semicolon;
2272 return OK;
2273}
2274
2275/*
2276 * Generate an instruction for any command modifiers.
2277 */
2278 int
2279generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
2280{
2281 isn_T *isn;
2282
2283 if (has_cmdmod(cmod, FALSE))
2284 {
2285 cctx->ctx_has_cmdmod = TRUE;
2286
2287 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
2288 return FAIL;
2289 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2290 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2291 return FAIL;
2292 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
2293 // filter program now belongs to the instruction
2294 cmod->cmod_filter_regmatch.regprog = NULL;
2295 }
2296
2297 return OK;
2298}
2299
2300 int
2301generate_undo_cmdmods(cctx_T *cctx)
2302{
2303 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2304 return FAIL;
2305 cctx->ctx_has_cmdmod = FALSE;
2306 return OK;
2307}
2308
2309/*
2310 * Generate a STORE instruction for "dest", not being "dest_local".
Bram Moolenaard505d172022-12-18 21:42:55 +00002311 * "lhs" might be NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002312 * Return FAIL when out of memory.
2313 */
2314 int
2315generate_store_var(
2316 cctx_T *cctx,
2317 assign_dest_T dest,
2318 int opt_flags,
2319 int vimvaridx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002320 type_T *type,
Bram Moolenaard505d172022-12-18 21:42:55 +00002321 char_u *name,
2322 lhs_T *lhs)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002323{
2324 switch (dest)
2325 {
2326 case dest_option:
2327 return generate_STOREOPT(cctx, ISN_STOREOPT,
2328 skip_option_env_lead(name), opt_flags);
2329 case dest_func_option:
2330 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
2331 skip_option_env_lead(name), opt_flags);
2332 case dest_global:
2333 // include g: with the name, easier to execute that way
2334 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
2335 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
2336 case dest_buffer:
2337 // include b: with the name, easier to execute that way
2338 return generate_STORE(cctx, ISN_STOREB, 0, name);
2339 case dest_window:
2340 // include w: with the name, easier to execute that way
2341 return generate_STORE(cctx, ISN_STOREW, 0, name);
2342 case dest_tab:
2343 // include t: with the name, easier to execute that way
2344 return generate_STORE(cctx, ISN_STORET, 0, name);
2345 case dest_env:
2346 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
2347 case dest_reg:
2348 return generate_STORE(cctx, ISN_STOREREG,
2349 name[1] == '@' ? '"' : name[1], NULL);
2350 case dest_vimvar:
2351 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
2352 case dest_script:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002353 {
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002354 int scriptvar_idx = lhs->lhs_scriptvar_idx;
2355 int scriptvar_sid = lhs->lhs_scriptvar_sid;
2356 if (scriptvar_idx < 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002357 {
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002358 isntype_T isn_type = ISN_STORES;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002359
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002360 if (SCRIPT_ID_VALID(scriptvar_sid)
2361 && SCRIPT_ITEM(scriptvar_sid)->sn_import_autoload
2362 && SCRIPT_ITEM(scriptvar_sid)->sn_autoload_prefix
2363 == NULL)
2364 {
2365 // "import autoload './dir/script.vim'" - load script
2366 // first
2367 if (generate_SOURCE(cctx, scriptvar_sid) == FAIL)
2368 return FAIL;
2369 isn_type = ISN_STOREEXPORT;
2370 }
2371
2372 // "s:" may be included in the name.
2373 return generate_OLDSCRIPT(cctx, isn_type, name,
2374 scriptvar_sid, type);
2375 }
2376 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002377 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002378 }
Bram Moolenaard505d172022-12-18 21:42:55 +00002379 case dest_class_member:
2380 return generate_CLASSMEMBER(cctx, FALSE,
2381 lhs->lhs_class, lhs->lhs_classmember_idx);
2382
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002383 case dest_local:
2384 case dest_expr:
2385 // cannot happen
2386 break;
2387 }
2388 return FAIL;
2389}
2390
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002391/*
2392 * Return TRUE when inside a "for" or "while" loop.
2393 */
2394 int
2395inside_loop_scope(cctx_T *cctx)
2396{
2397 scope_T *scope = cctx->ctx_scope;
2398
2399 for (;;)
2400 {
2401 if (scope == NULL)
2402 break;
2403 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
2404 return TRUE;
2405 scope = scope->se_outer;
2406 }
2407 return FALSE;
2408}
2409
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002410 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002411generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002412{
2413 if (lhs->lhs_dest != dest_local)
2414 return generate_store_var(cctx, lhs->lhs_dest,
2415 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
Bram Moolenaard505d172022-12-18 21:42:55 +00002416 lhs->lhs_type, lhs->lhs_name, lhs);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002417
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002418 if (lhs->lhs_lvar == NULL)
2419 return OK;
2420
2421 garray_T *instr = &cctx->ctx_instr;
2422 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2423
2424 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
2425 // ISN_STORENR.
2426 // And "var = 0" does not need any instruction.
2427 if (lhs->lhs_lvar->lv_from_outer == 0
2428 && instr->ga_len == instr_count + 1
2429 && isn->isn_type == ISN_PUSHNR)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002430 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002431 varnumber_T val = isn->isn_arg.number;
2432 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002433
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002434 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002435 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002436 // zero is the default value, no need to do anything
2437 --instr->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002438 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002439 else
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002440 {
2441 isn->isn_type = ISN_STORENR;
2442 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
2443 isn->isn_arg.storenr.stnr_val = val;
2444 }
2445 if (stack->ga_len > 0)
2446 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002447 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002448 else if (lhs->lhs_lvar->lv_from_outer > 0)
2449 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
2450 lhs->lhs_lvar->lv_from_outer, lhs->lhs_lvar->lv_loop_idx);
2451 else
2452 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002453 return OK;
2454}
2455
2456#if defined(FEAT_PROFILE) || defined(PROTO)
2457 void
2458may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2459{
2460 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
2461 generate_instr(cctx, ISN_PROF_END);
2462}
2463#endif
2464
2465
2466/*
2467 * Delete an instruction, free what it contains.
2468 */
2469 void
2470delete_instr(isn_T *isn)
2471{
2472 switch (isn->isn_type)
2473 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002474 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002475 case ISN_DEF:
2476 case ISN_EXEC:
2477 case ISN_EXECRANGE:
2478 case ISN_EXEC_SPLIT:
2479 case ISN_LEGACY_EVAL:
2480 case ISN_LOADAUTO:
2481 case ISN_LOADB:
2482 case ISN_LOADENV:
2483 case ISN_LOADG:
2484 case ISN_LOADOPT:
2485 case ISN_LOADT:
2486 case ISN_LOADW:
2487 case ISN_LOCKUNLOCK:
2488 case ISN_PUSHEXC:
2489 case ISN_PUSHFUNC:
2490 case ISN_PUSHS:
2491 case ISN_RANGE:
2492 case ISN_STOREAUTO:
2493 case ISN_STOREB:
2494 case ISN_STOREENV:
2495 case ISN_STOREG:
2496 case ISN_STORET:
2497 case ISN_STOREW:
2498 case ISN_STRINGMEMBER:
2499 vim_free(isn->isn_arg.string);
2500 break;
2501
2502 case ISN_SUBSTITUTE:
2503 {
2504 int idx;
2505 isn_T *list = isn->isn_arg.subs.subs_instr;
2506
2507 vim_free(isn->isn_arg.subs.subs_cmd);
2508 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2509 delete_instr(list + idx);
2510 vim_free(list);
2511 }
2512 break;
2513
2514 case ISN_INSTR:
2515 {
2516 int idx;
2517 isn_T *list = isn->isn_arg.instr;
2518
2519 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2520 delete_instr(list + idx);
2521 vim_free(list);
2522 }
2523 break;
2524
2525 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002526 case ISN_LOADEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002527 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002528 case ISN_STOREEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002529 vim_free(isn->isn_arg.loadstore.ls_name);
2530 break;
2531
2532 case ISN_UNLET:
2533 case ISN_UNLETENV:
2534 vim_free(isn->isn_arg.unlet.ul_name);
2535 break;
2536
2537 case ISN_STOREOPT:
2538 case ISN_STOREFUNCOPT:
2539 vim_free(isn->isn_arg.storeopt.so_name);
2540 break;
2541
2542 case ISN_PUSHBLOB: // push blob isn_arg.blob
2543 blob_unref(isn->isn_arg.blob);
2544 break;
2545
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002546 case ISN_PUSHCLASS:
Bram Moolenaar30a84472023-02-27 08:07:14 +00002547 class_unref(isn->isn_arg.classarg);
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002548 break;
2549
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002550 case ISN_UCALL:
2551 vim_free(isn->isn_arg.ufunc.cuf_name);
2552 break;
2553
2554 case ISN_FUNCREF:
2555 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002556 funcref_T *funcref = &isn->isn_arg.funcref;
2557 funcref_extra_T *extra = funcref->fr_extra;
2558
2559 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002560 {
2561 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002562 + funcref->fr_dfunc_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002563 ufunc_T *ufunc = dfunc->df_ufunc;
2564
2565 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2566 func_ptr_unref(ufunc);
2567 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002568 if (extra != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002569 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002570 char_u *name = extra->fre_func_name;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002571
2572 if (name != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002573 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002574 func_unref(name);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002575 vim_free(name);
2576 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00002577 if (extra->fre_class != NULL)
2578 class_unref(extra->fre_class);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002579 vim_free(extra);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002580 }
2581 }
2582 break;
2583
2584 case ISN_DCALL:
2585 {
2586 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002587 + isn->isn_arg.dfunc.cdf_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002588
2589 if (dfunc->df_ufunc != NULL
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002590 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002591 func_ptr_unref(dfunc->df_ufunc);
2592 }
2593 break;
2594
Bram Moolenaard0200c82023-01-28 15:19:40 +00002595 case ISN_METHODCALL:
2596 {
2597 cmfunc_T *mfunc = isn->isn_arg.mfunc;
2598 class_unref(mfunc->cmf_itf);
2599 vim_free(mfunc);
2600 }
2601 break;
2602
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002603 case ISN_NEWFUNC:
2604 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002605 newfuncarg_T *arg = isn->isn_arg.newfunc.nf_arg;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002606
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002607 if (arg != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002608 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002609 ufunc_T *ufunc = find_func_even_dead(
2610 arg->nfa_lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002611
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002612 if (ufunc != NULL)
2613 {
2614 unlink_def_function(ufunc);
2615 func_ptr_unref(ufunc);
2616 }
2617
2618 vim_free(arg->nfa_lambda);
2619 vim_free(arg->nfa_global);
2620 vim_free(arg);
2621 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002622 }
2623 break;
2624
2625 case ISN_CHECKTYPE:
2626 case ISN_SETTYPE:
2627 free_type(isn->isn_arg.type.ct_type);
2628 break;
2629
2630 case ISN_CMDMOD:
2631 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002632 ->cmod_filter_regmatch.regprog);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002633 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2634 break;
2635
2636 case ISN_LOADSCRIPT:
2637 case ISN_STORESCRIPT:
2638 vim_free(isn->isn_arg.script.scriptref);
2639 break;
2640
Bram Moolenaard505d172022-12-18 21:42:55 +00002641 case ISN_LOAD_CLASSMEMBER:
2642 case ISN_STORE_CLASSMEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00002643 case ISN_GET_ITF_MEMBER:
Bram Moolenaard505d172022-12-18 21:42:55 +00002644 class_unref(isn->isn_arg.classmember.cm_class);
2645 break;
2646
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002647 case ISN_STOREINDEX:
2648 class_unref(isn->isn_arg.storeindex.si_class);
2649 break;
2650
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002651 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002652 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002653 break;
2654
2655 case ISN_CEXPR_CORE:
2656 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2657 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2658 break;
2659
2660 case ISN_2BOOL:
2661 case ISN_2STRING:
2662 case ISN_2STRING_ANY:
2663 case ISN_ADDBLOB:
2664 case ISN_ADDLIST:
2665 case ISN_ANYINDEX:
2666 case ISN_ANYSLICE:
2667 case ISN_BCALL:
2668 case ISN_BLOBAPPEND:
2669 case ISN_BLOBINDEX:
2670 case ISN_BLOBSLICE:
2671 case ISN_CATCH:
2672 case ISN_CEXPR_AUCMD:
2673 case ISN_CHECKLEN:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002674 case ISN_CLEARDICT:
2675 case ISN_CMDMOD_REV:
2676 case ISN_COMPAREANY:
2677 case ISN_COMPAREBLOB:
2678 case ISN_COMPAREBOOL:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002679 case ISN_COMPARECLASS:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002680 case ISN_COMPAREDICT:
2681 case ISN_COMPAREFLOAT:
2682 case ISN_COMPAREFUNC:
2683 case ISN_COMPARELIST:
2684 case ISN_COMPARENR:
Bram Moolenaar7a222242022-03-01 19:23:24 +00002685 case ISN_COMPARENULL:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002686 case ISN_COMPAREOBJECT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002687 case ISN_COMPARESPECIAL:
2688 case ISN_COMPARESTRING:
2689 case ISN_CONCAT:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002690 case ISN_CONSTRUCT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002691 case ISN_COND2BOOL:
2692 case ISN_DEBUG:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002693 case ISN_DEFER:
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00002694 case ISN_DEFEROBJ:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002695 case ISN_DROP:
2696 case ISN_ECHO:
2697 case ISN_ECHOCONSOLE:
2698 case ISN_ECHOERR:
2699 case ISN_ECHOMSG:
Bram Moolenaar68a635a2022-09-01 17:26:17 +01002700 case ISN_ECHOWINDOW:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002701 case ISN_ENDLOOP:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002702 case ISN_ENDTRY:
2703 case ISN_EXECCONCAT:
2704 case ISN_EXECUTE:
2705 case ISN_FINALLY:
2706 case ISN_FINISH:
2707 case ISN_FOR:
2708 case ISN_GETITEM:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002709 case ISN_GET_OBJ_MEMBER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002710 case ISN_JUMP:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002711 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002712 case ISN_JUMP_IF_ARG_SET:
2713 case ISN_LISTAPPEND:
2714 case ISN_LISTINDEX:
2715 case ISN_LISTSLICE:
2716 case ISN_LOAD:
2717 case ISN_LOADBDICT:
2718 case ISN_LOADGDICT:
2719 case ISN_LOADOUTER:
2720 case ISN_LOADREG:
2721 case ISN_LOADTDICT:
2722 case ISN_LOADV:
2723 case ISN_LOADWDICT:
2724 case ISN_LOCKCONST:
2725 case ISN_MEMBER:
2726 case ISN_NEGATENR:
2727 case ISN_NEWDICT:
2728 case ISN_NEWLIST:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002729 case ISN_NEWPARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002730 case ISN_OPANY:
2731 case ISN_OPFLOAT:
2732 case ISN_OPNR:
2733 case ISN_PCALL:
2734 case ISN_PCALL_END:
2735 case ISN_PROF_END:
2736 case ISN_PROF_START:
2737 case ISN_PUSHBOOL:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002738 case ISN_PUSHCHANNEL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002739 case ISN_PUSHF:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002740 case ISN_PUSHJOB:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002741 case ISN_PUSHNR:
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002742 case ISN_PUSHOBJ:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002743 case ISN_PUSHSPEC:
2744 case ISN_PUT:
2745 case ISN_REDIREND:
2746 case ISN_REDIRSTART:
2747 case ISN_RETURN:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002748 case ISN_RETURN_OBJECT:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002749 case ISN_RETURN_VOID:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002750 case ISN_SHUFFLE:
2751 case ISN_SLICE:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002752 case ISN_SOURCE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002753 case ISN_STORE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002754 case ISN_STORENR:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002755 case ISN_STOREOUTER:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002756 case ISN_STORE_THIS:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002757 case ISN_STORERANGE:
2758 case ISN_STOREREG:
2759 case ISN_STOREV:
2760 case ISN_STRINDEX:
2761 case ISN_STRSLICE:
2762 case ISN_THROW:
2763 case ISN_TRYCONT:
2764 case ISN_UNLETINDEX:
2765 case ISN_UNLETRANGE:
2766 case ISN_UNPACK:
2767 case ISN_USEDICT:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002768 case ISN_WHILE:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002769 // nothing allocated
2770 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002771 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002772}
2773
2774 void
2775clear_instr_ga(garray_T *gap)
2776{
2777 int idx;
2778
2779 for (idx = 0; idx < gap->ga_len; ++idx)
2780 delete_instr(((isn_T *)gap->ga_data) + idx);
2781 ga_clear(gap);
2782}
2783
2784
2785#endif // defined(FEAT_EVAL)