blob: 3e9c6b6e8eb35e0668fa5480898d1737ce79ddca [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
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000139generate_GET_OBJ_MEMBER(cctx_T *cctx, int idx, type_T *type)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000140{
141 RETURN_OK_IF_SKIP(cctx);
142
143 // drop the object type
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000144 isn_T *isn = generate_instr_drop(cctx, ISN_GET_OBJ_MEMBER, 1);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000145 if (isn == NULL)
146 return FAIL;
147
148 isn->isn_arg.number = idx;
149 return push_type_stack2(cctx, type, &t_any);
150}
151
152/*
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000153 * Generate ISN_GET_ITF_MEMBER - access member of interface at bottom of stack
154 * by index.
155 */
156 int
157generate_GET_ITF_MEMBER(cctx_T *cctx, class_T *itf, int idx, type_T *type)
158{
159 RETURN_OK_IF_SKIP(cctx);
160
161 // drop the object type
162 isn_T *isn = generate_instr_drop(cctx, ISN_GET_ITF_MEMBER, 1);
163 if (isn == NULL)
164 return FAIL;
165
166 isn->isn_arg.classmember.cm_class = itf;
167 ++itf->class_refcount;
168 isn->isn_arg.classmember.cm_idx = idx;
169 return push_type_stack2(cctx, type, &t_any);
170}
171
172/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000173 * Generate ISN_STORE_THIS - store value in member of "this" object with member
174 * index "idx".
175 */
176 int
177generate_STORE_THIS(cctx_T *cctx, int idx)
178{
179 RETURN_OK_IF_SKIP(cctx);
180
181 // drop the value type
182 isn_T *isn = generate_instr_drop(cctx, ISN_STORE_THIS, 1);
183 if (isn == NULL)
184 return FAIL;
185
186 isn->isn_arg.number = idx;
187 return OK;
188}
189
190/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000191 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
192 * But only for simple types.
193 * When "tolerant" is TRUE convert most types to string, e.g. a List.
194 */
195 int
196may_generate_2STRING(int offset, int tolerant, cctx_T *cctx)
197{
198 isn_T *isn;
199 isntype_T isntype = ISN_2STRING;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000200 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000201
202 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +0000203 type = get_type_on_stack(cctx, -1 - offset);
204 switch (type->tt_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000205 {
206 // nothing to be done
207 case VAR_STRING: return OK;
208
209 // conversion possible
210 case VAR_SPECIAL:
211 case VAR_BOOL:
212 case VAR_NUMBER:
213 case VAR_FLOAT:
214 break;
215
216 // conversion possible (with runtime check)
217 case VAR_ANY:
218 case VAR_UNKNOWN:
219 isntype = ISN_2STRING_ANY;
220 break;
221
222 // conversion possible when tolerant
223 case VAR_LIST:
224 if (tolerant)
225 {
226 isntype = ISN_2STRING_ANY;
227 break;
228 }
229 // FALLTHROUGH
230
231 // conversion not possible
232 case VAR_VOID:
233 case VAR_BLOB:
234 case VAR_FUNC:
235 case VAR_PARTIAL:
236 case VAR_DICT:
237 case VAR_JOB:
238 case VAR_CHANNEL:
239 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000240 case VAR_CLASS:
241 case VAR_OBJECT:
Bram Moolenaar078a4612022-01-04 15:17:03 +0000242 to_string_error(type->tt_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000243 return FAIL;
244 }
245
Bram Moolenaar078a4612022-01-04 15:17:03 +0000246 set_type_on_stack(cctx, &t_string, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000247 if ((isn = generate_instr(cctx, isntype)) == NULL)
248 return FAIL;
249 isn->isn_arg.tostring.offset = offset;
250 isn->isn_arg.tostring.tolerant = tolerant;
251
252 return OK;
253}
254
255 static int
256check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
257{
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000258 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT
259 || type1 == VAR_ANY || type1 == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000260 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000261 || type2 == VAR_ANY || type2 == VAR_UNKNOWN)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000262 {
263 if (*op == '+')
264 emsg(_(e_wrong_argument_type_for_plus));
265 else
266 semsg(_(e_char_requires_number_or_float_arguments), *op);
267 return FAIL;
268 }
269 return OK;
270}
271
272/*
273 * Generate instruction for "+". For a list this creates a new list.
274 */
275 int
276generate_add_instr(
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000277 cctx_T *cctx,
278 vartype_T vartype,
279 type_T *type1,
280 type_T *type2,
281 exprtype_T expr_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000282{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000283 isn_T *isn = generate_instr_drop(cctx,
284 vartype == VAR_NUMBER ? ISN_OPNR
285 : vartype == VAR_LIST ? ISN_ADDLIST
286 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000287 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000288 : ISN_OPANY, 1);
289
290 if (vartype != VAR_LIST && vartype != VAR_BLOB
291 && type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000292 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000293 && type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000294 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000295 && check_number_or_float(
296 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
297 return FAIL;
298
299 if (isn != NULL)
300 {
301 if (isn->isn_type == ISN_ADDLIST)
302 isn->isn_arg.op.op_type = expr_type;
303 else
304 isn->isn_arg.op.op_type = EXPR_ADD;
305 }
306
307 // When concatenating two lists with different member types the member type
308 // becomes "any".
309 if (vartype == VAR_LIST
310 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
311 && type1->tt_member != type2->tt_member)
Bram Moolenaar078a4612022-01-04 15:17:03 +0000312 set_type_on_stack(cctx, &t_list_any, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000313
314 return isn == NULL ? FAIL : OK;
315}
316
317/*
318 * Get the type to use for an instruction for an operation on "type1" and
319 * "type2". If they are matching use a type-specific instruction. Otherwise
320 * fall back to runtime type checking.
321 */
322 vartype_T
323operator_type(type_T *type1, type_T *type2)
324{
325 if (type1->tt_type == type2->tt_type
326 && (type1->tt_type == VAR_NUMBER
327 || type1->tt_type == VAR_LIST
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000328 || type1->tt_type == VAR_FLOAT
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000329 || type1->tt_type == VAR_BLOB))
330 return type1->tt_type;
331 return VAR_ANY;
332}
333
334/*
335 * Generate an instruction with two arguments. The instruction depends on the
336 * type of the arguments.
337 */
338 int
339generate_two_op(cctx_T *cctx, char_u *op)
340{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000341 type_T *type1;
342 type_T *type2;
343 vartype_T vartype;
344 isn_T *isn;
345
346 RETURN_OK_IF_SKIP(cctx);
347
348 // Get the known type of the two items on the stack.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000349 type1 = get_type_on_stack(cctx, 1);
350 type2 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000351 vartype = operator_type(type1, type2);
352
353 switch (*op)
354 {
355 case '+':
356 if (generate_add_instr(cctx, vartype, type1, type2,
357 EXPR_COPY) == FAIL)
358 return FAIL;
359 break;
360
361 case '-':
362 case '*':
363 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
364 op) == FAIL)
365 return FAIL;
366 if (vartype == VAR_NUMBER)
367 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000368 else if (vartype == VAR_FLOAT)
369 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000370 else
371 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
372 if (isn != NULL)
373 isn->isn_arg.op.op_type = *op == '*'
374 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
375 break;
376
377 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000378 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000379 && type1->tt_type != VAR_NUMBER)
380 || (type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000381 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000382 && type2->tt_type != VAR_NUMBER))
383 {
384 emsg(_(e_percent_requires_number_arguments));
385 return FAIL;
386 }
387 isn = generate_instr_drop(cctx,
388 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
389 if (isn != NULL)
390 isn->isn_arg.op.op_type = EXPR_REM;
391 break;
392 }
393
394 // correct type of result
395 if (vartype == VAR_ANY)
396 {
397 type_T *type = &t_any;
398
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000399 // float+number and number+float results in float
400 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100401 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000402 type = &t_float;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000403 set_type_on_stack(cctx, type, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000404 }
405
406 return OK;
407}
408
409/*
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000410 * Get the instruction to use for comparing two values with specified types.
411 * Either "tv1" and "tv2" are passed or "type1" and "type2".
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000412 * Return ISN_DROP when failed.
413 */
414 static isntype_T
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000415get_compare_isn(
Bram Moolenaar2ed57ac2023-04-01 22:05:38 +0100416 exprtype_T exprtype,
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000417 typval_T *tv1,
418 typval_T *tv2,
419 type_T *type1,
420 type_T *type2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000421{
422 isntype_T isntype = ISN_DROP;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000423 vartype_T vartype1 = tv1 != NULL ? tv1->v_type : type1->tt_type;
424 vartype_T vartype2 = tv2 != NULL ? tv2->v_type : type2->tt_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000425
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000426 if (vartype1 == vartype2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000427 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000428 switch (vartype1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000429 {
430 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
431 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
432 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
433 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
434 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
435 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
436 case VAR_LIST: isntype = ISN_COMPARELIST; break;
437 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
438 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000439 case VAR_CLASS: isntype = ISN_COMPARECLASS; break;
440 case VAR_OBJECT: isntype = ISN_COMPAREOBJECT; break;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000441 default: isntype = ISN_COMPAREANY; break;
442 }
443 }
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000444 else if (vartype1 == VAR_ANY || vartype2 == VAR_ANY
445 || ((vartype1 == VAR_NUMBER || vartype1 == VAR_FLOAT)
446 && (vartype2 == VAR_NUMBER || vartype2 == VAR_FLOAT))
447 || (vartype1 == VAR_FUNC && vartype2 == VAR_PARTIAL)
448 || (vartype1 == VAR_PARTIAL && vartype2 == VAR_FUNC))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000449 isntype = ISN_COMPAREANY;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000450 else if (vartype1 == VAR_SPECIAL || vartype2 == VAR_SPECIAL)
Bram Moolenaar7a222242022-03-01 19:23:24 +0000451 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000452 if ((vartype1 == VAR_SPECIAL
453 && (tv1 != NULL ? tv1->vval.v_number == VVAL_NONE
454 : type1 == &t_none)
455 && vartype2 != VAR_STRING)
456 || (vartype2 == VAR_SPECIAL
457 && (tv2 != NULL ? tv2->vval.v_number == VVAL_NONE
458 : type2 == &t_none)
459 && vartype1 != VAR_STRING))
460 {
461 semsg(_(e_cannot_compare_str_with_str),
462 vartype_name(vartype1), vartype_name(vartype2));
463 return ISN_DROP;
464 }
Bram Moolenaar05667812022-03-15 20:21:33 +0000465 // although comparing null with number, float or bool is not useful, we
466 // allow it
Bram Moolenaar7a222242022-03-01 19:23:24 +0000467 isntype = ISN_COMPARENULL;
468 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000469
470 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
471 && (isntype == ISN_COMPAREBOOL
472 || isntype == ISN_COMPARESPECIAL
473 || isntype == ISN_COMPARENR
474 || isntype == ISN_COMPAREFLOAT))
475 {
476 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000477 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(vartype1));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000478 return ISN_DROP;
479 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000480 if (!(exprtype == EXPR_IS || exprtype == EXPR_ISNOT
481 || exprtype == EXPR_EQUAL || exprtype == EXPR_NEQUAL)
482 && (isntype == ISN_COMPAREOBJECT || isntype == ISN_COMPARECLASS))
483 {
484 semsg(_(e_invalid_operation_for_str), vartype_name(vartype1));
485 return ISN_DROP;
486 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000487 if (isntype == ISN_DROP
Bram Moolenaar2ed57ac2023-04-01 22:05:38 +0100488 || (isntype != ISN_COMPARENULL
489 && (((exprtype != EXPR_EQUAL
490 && exprtype != EXPR_NEQUAL
491 && (vartype1 == VAR_BOOL || vartype1 == VAR_SPECIAL
492 || vartype2 == VAR_BOOL || vartype2 == VAR_SPECIAL)))
493 || ((exprtype != EXPR_EQUAL
494 && exprtype != EXPR_NEQUAL
495 && exprtype != EXPR_IS
496 && exprtype != EXPR_ISNOT
497 && (vartype1 == VAR_BLOB || vartype2 == VAR_BLOB
498 || vartype1 == VAR_LIST || vartype2 == VAR_LIST))))))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000499 {
500 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000501 vartype_name(vartype1), vartype_name(vartype2));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000502 return ISN_DROP;
503 }
504 return isntype;
505}
506
507 int
508check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
509{
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000510 if (get_compare_isn(type, tv1, tv2, NULL, NULL) == ISN_DROP)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000511 return FAIL;
512 return OK;
513}
514
515/*
516 * Generate an ISN_COMPARE* instruction with a boolean result.
517 */
518 int
519generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
520{
521 isntype_T isntype;
522 isn_T *isn;
523 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000524
525 RETURN_OK_IF_SKIP(cctx);
526
527 // Get the known type of the two items on the stack. If they are matching
528 // use a type-specific instruction. Otherwise fall back to runtime type
529 // checking.
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000530 isntype = get_compare_isn(exprtype, NULL, NULL,
531 get_type_on_stack(cctx, 1), get_type_on_stack(cctx, 0));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000532 if (isntype == ISN_DROP)
533 return FAIL;
534
535 if ((isn = generate_instr(cctx, isntype)) == NULL)
536 return FAIL;
537 isn->isn_arg.op.op_type = exprtype;
538 isn->isn_arg.op.op_ic = ic;
539
540 // takes two arguments, puts one bool back
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100541 --stack->ga_len;
542 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000543
544 return OK;
545}
546
547/*
LemonBoy372bcce2022-04-25 12:43:20 +0100548 * Generate an ISN_CONCAT instruction.
549 * "count" is the number of stack elements to join together and it must be
550 * greater or equal to one.
551 * The caller ensures all the "count" elements on the stack have the right type.
552 */
553 int
554generate_CONCAT(cctx_T *cctx, int count)
555{
556 isn_T *isn;
557 garray_T *stack = &cctx->ctx_type_stack;
558
559 RETURN_OK_IF_SKIP(cctx);
560
LemonBoy372bcce2022-04-25 12:43:20 +0100561 if ((isn = generate_instr(cctx, ISN_CONCAT)) == NULL)
562 return FAIL;
563 isn->isn_arg.number = count;
564
565 // drop the argument types
566 stack->ga_len -= count - 1;
567
568 return OK;
569}
570
571/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000572 * Generate an ISN_2BOOL instruction.
573 * "offset" is the offset in the type stack.
574 */
575 int
576generate_2BOOL(cctx_T *cctx, int invert, int offset)
577{
578 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000579
580 RETURN_OK_IF_SKIP(cctx);
581 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
582 return FAIL;
583 isn->isn_arg.tobool.invert = invert;
584 isn->isn_arg.tobool.offset = offset;
585
586 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000587 set_type_on_stack(cctx, &t_bool, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000588
589 return OK;
590}
591
592/*
593 * Generate an ISN_COND2BOOL instruction.
594 */
595 int
596generate_COND2BOOL(cctx_T *cctx)
597{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000598 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100599 if (generate_instr(cctx, ISN_COND2BOOL) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000600 return FAIL;
601
602 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000603 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000604
605 return OK;
606}
607
608 int
609generate_TYPECHECK(
610 cctx_T *cctx,
611 type_T *expected,
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000612 int number_ok, // add TTFLAG_NUMBER_OK flag
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000613 int offset,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100614 int is_var,
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000615 int argidx)
616{
617 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000618
619 RETURN_OK_IF_SKIP(cctx);
620 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
621 return FAIL;
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000622 type_T *tt;
623 if (expected->tt_type == VAR_FLOAT && number_ok)
624 {
625 // always allocate, also for static types
626 tt = ALLOC_ONE(type_T);
627 if (tt != NULL)
628 {
629 *tt = *expected;
Bram Moolenaarc4b3f642022-12-30 10:36:34 +0000630 tt->tt_flags &= ~TTFLAG_STATIC;
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000631 tt->tt_flags |= TTFLAG_NUMBER_OK;
632 }
633 }
634 else
635 tt = alloc_type(expected);
636
637 isn->isn_arg.type.ct_type = tt;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000638 isn->isn_arg.type.ct_off = (int8_T)offset;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100639 isn->isn_arg.type.ct_is_var = is_var;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000640 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
641
642 // type becomes expected
Bram Moolenaar078a4612022-01-04 15:17:03 +0000643 set_type_on_stack(cctx, expected, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000644
645 return OK;
646}
647
648 int
649generate_SETTYPE(
650 cctx_T *cctx,
651 type_T *expected)
652{
653 isn_T *isn;
654
655 RETURN_OK_IF_SKIP(cctx);
656 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
657 return FAIL;
658 isn->isn_arg.type.ct_type = alloc_type(expected);
659 return OK;
660}
661
662/*
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000663 * Generate an ISN_PUSHOBJ instruction. Object is always NULL.
664 */
Ernie Rael5c018be2023-08-27 18:40:26 +0200665 int
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000666generate_PUSHOBJ(cctx_T *cctx)
667{
668 RETURN_OK_IF_SKIP(cctx);
Ernie Rael5c018be2023-08-27 18:40:26 +0200669 if (generate_instr_type(cctx, ISN_PUSHOBJ, &t_object) == NULL)
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000670 return FAIL;
671 return OK;
672}
673
674/*
675 * Generate an ISN_PUSHCLASS instruction. "class" can be NULL.
676 */
677 static int
678generate_PUSHCLASS(cctx_T *cctx, class_T *class)
679{
680 RETURN_OK_IF_SKIP(cctx);
681 isn_T *isn = generate_instr_type(cctx, ISN_PUSHCLASS,
682 class == NULL ? &t_any : &class->class_type);
683 if (isn == NULL)
684 return FAIL;
Bram Moolenaar30a84472023-02-27 08:07:14 +0000685 isn->isn_arg.classarg = class;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000686 if (class != NULL)
687 ++class->class_refcount;
688 return OK;
689}
690
691/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000692 * Generate a PUSH instruction for "tv".
693 * "tv" will be consumed or cleared.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000694 */
695 int
696generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
697{
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100698 switch (tv->v_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000699 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100700 case VAR_BOOL:
701 generate_PUSHBOOL(cctx, tv->vval.v_number);
702 break;
703 case VAR_SPECIAL:
704 generate_PUSHSPEC(cctx, tv->vval.v_number);
705 break;
706 case VAR_NUMBER:
707 generate_PUSHNR(cctx, tv->vval.v_number);
708 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100709 case VAR_FLOAT:
710 generate_PUSHF(cctx, tv->vval.v_float);
711 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100712 case VAR_BLOB:
713 generate_PUSHBLOB(cctx, tv->vval.v_blob);
714 tv->vval.v_blob = NULL;
715 break;
716 case VAR_LIST:
717 if (tv->vval.v_list != NULL)
718 iemsg("non-empty list constant not supported");
719 generate_NEWLIST(cctx, 0, TRUE);
720 break;
721 case VAR_DICT:
722 if (tv->vval.v_dict != NULL)
723 iemsg("non-empty dict constant not supported");
724 generate_NEWDICT(cctx, 0, TRUE);
725 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000726#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100727 case VAR_JOB:
728 if (tv->vval.v_job != NULL)
729 iemsg("non-null job constant not supported");
730 generate_PUSHJOB(cctx);
731 break;
732 case VAR_CHANNEL:
733 if (tv->vval.v_channel != NULL)
734 iemsg("non-null channel constant not supported");
735 generate_PUSHCHANNEL(cctx);
736 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000737#endif
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100738 case VAR_FUNC:
739 if (tv->vval.v_string != NULL)
740 iemsg("non-null function constant not supported");
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100741 generate_PUSHFUNC(cctx, NULL, &t_func_unknown, TRUE);
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100742 break;
743 case VAR_PARTIAL:
744 if (tv->vval.v_partial != NULL)
745 iemsg("non-null partial constant not supported");
746 if (generate_instr_type(cctx, ISN_NEWPARTIAL, &t_func_unknown)
747 == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000748 return FAIL;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100749 break;
750 case VAR_STRING:
751 generate_PUSHS(cctx, &tv->vval.v_string);
752 tv->vval.v_string = NULL;
753 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000754 case VAR_OBJECT:
755 if (tv->vval.v_object != NULL)
756 {
757 emsg(_(e_cannot_use_non_null_object));
758 return FAIL;
759 }
760 generate_PUSHOBJ(cctx);
761 break;
762 case VAR_CLASS:
763 generate_PUSHCLASS(cctx, tv->vval.v_class);
764 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100765 default:
766 siemsg("constant type %d not supported", tv->v_type);
767 clear_tv(tv);
768 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000769 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100770 tv->v_type = VAR_UNKNOWN;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000771 return OK;
772}
773
774/*
775 * Generate an ISN_PUSHNR instruction.
776 */
777 int
778generate_PUSHNR(cctx_T *cctx, varnumber_T number)
779{
780 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000781
782 RETURN_OK_IF_SKIP(cctx);
783 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
784 return FAIL;
785 isn->isn_arg.number = number;
786
787 if (number == 0 || number == 1)
788 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000789 set_type_on_stack(cctx, &t_number_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000790 return OK;
791}
792
793/*
794 * Generate an ISN_PUSHBOOL instruction.
795 */
796 int
797generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
798{
799 isn_T *isn;
800
801 RETURN_OK_IF_SKIP(cctx);
802 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
803 return FAIL;
804 isn->isn_arg.number = number;
805
806 return OK;
807}
808
809/*
810 * Generate an ISN_PUSHSPEC instruction.
811 */
812 int
813generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
814{
815 isn_T *isn;
816
817 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000818 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC,
819 number == VVAL_NULL ? &t_null : &t_none)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000820 return FAIL;
821 isn->isn_arg.number = number;
822
823 return OK;
824}
825
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000826/*
827 * Generate an ISN_PUSHF instruction.
828 */
829 int
830generate_PUSHF(cctx_T *cctx, float_T fnumber)
831{
832 isn_T *isn;
833
834 RETURN_OK_IF_SKIP(cctx);
835 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
836 return FAIL;
837 isn->isn_arg.fnumber = fnumber;
838
839 return OK;
840}
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000841
842/*
843 * Generate an ISN_PUSHS instruction.
844 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100845 * Note that if "str" is used in the instruction OK is returned and "*str" is
846 * not set to NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000847 */
848 int
849generate_PUSHS(cctx_T *cctx, char_u **str)
850{
851 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100852 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000853
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100854 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000855 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100856 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
857 ret = FAIL;
858 else
859 {
860 isn->isn_arg.string = str == NULL ? NULL : *str;
861 return OK;
862 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000863 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100864 if (str != NULL)
865 VIM_CLEAR(*str);
866 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000867}
868
869/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000870 * Generate an ISN_PUSHCHANNEL instruction. Channel is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000871 */
872 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000873generate_PUSHCHANNEL(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000874{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000875 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100876#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100877 if (generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000878 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000879 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100880#else
881 emsg(_(e_channel_job_feature_not_available));
882 return FAIL;
883#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000884}
885
886/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000887 * Generate an ISN_PUSHJOB instruction. Job is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000888 */
889 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000890generate_PUSHJOB(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000891{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000892 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100893#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100894 if (generate_instr_type(cctx, ISN_PUSHJOB, &t_job) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000895 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000896 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100897#else
898 emsg(_(e_channel_job_feature_not_available));
899 return FAIL;
900#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000901}
902
903/*
904 * Generate an ISN_PUSHBLOB instruction.
905 * Consumes "blob".
906 */
907 int
908generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
909{
910 isn_T *isn;
911
912 RETURN_OK_IF_SKIP(cctx);
913 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
914 return FAIL;
915 isn->isn_arg.blob = blob;
916
917 return OK;
918}
919
920/*
921 * Generate an ISN_PUSHFUNC instruction with name "name".
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100922 * When "may_prefix" is TRUE prefix "g:" unless "name" is script-local or
923 * autoload.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000924 */
925 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100926generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type, int may_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000927{
928 isn_T *isn;
929 char_u *funcname;
930
931 RETURN_OK_IF_SKIP(cctx);
932 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
933 return FAIL;
934 if (name == NULL)
935 funcname = NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100936 else if (!may_prefix
937 || *name == K_SPECIAL // script-local
Bram Moolenaard041f422022-01-12 19:54:00 +0000938 || vim_strchr(name, AUTOLOAD_CHAR) != NULL) // autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000939 funcname = vim_strsave(name);
940 else
941 {
942 funcname = alloc(STRLEN(name) + 3);
943 if (funcname != NULL)
944 {
945 STRCPY(funcname, "g:");
946 STRCPY(funcname + 2, name);
947 }
948 }
949
950 isn->isn_arg.string = funcname;
951 return OK;
952}
953
954/*
Bram Moolenaar06b77222022-01-25 15:51:56 +0000955 * Generate an ISN_AUTOLOAD instruction.
956 */
957 int
958generate_AUTOLOAD(cctx_T *cctx, char_u *name, type_T *type)
959{
960 isn_T *isn;
961
962 RETURN_OK_IF_SKIP(cctx);
963 if ((isn = generate_instr_type(cctx, ISN_AUTOLOAD, type)) == NULL)
964 return FAIL;
965 isn->isn_arg.string = vim_strsave(name);
966 if (isn->isn_arg.string == NULL)
967 return FAIL;
968 return OK;
969}
970
971/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000972 * Generate an ISN_GETITEM instruction with "index".
973 * "with_op" is TRUE for "+=" and other operators, the stack has the current
974 * value below the list with values.
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100975 * Caller must check the type is a list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000976 */
977 int
978generate_GETITEM(cctx_T *cctx, int index, int with_op)
979{
980 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000981 type_T *type = get_type_on_stack(cctx, with_op ? 1 : 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000982 type_T *item_type = &t_any;
983
984 RETURN_OK_IF_SKIP(cctx);
985
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000986 item_type = type->tt_member;
987 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
988 return FAIL;
989 isn->isn_arg.getitem.gi_index = index;
990 isn->isn_arg.getitem.gi_with_op = with_op;
991
992 // add the item type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +0000993 return push_type_stack(cctx, item_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000994}
995
996/*
997 * Generate an ISN_SLICE instruction with "count".
998 */
999 int
1000generate_SLICE(cctx_T *cctx, int count)
1001{
1002 isn_T *isn;
1003
1004 RETURN_OK_IF_SKIP(cctx);
1005 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1006 return FAIL;
1007 isn->isn_arg.number = count;
1008 return OK;
1009}
1010
1011/*
1012 * Generate an ISN_CHECKLEN instruction with "min_len".
1013 */
1014 int
1015generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1016{
1017 isn_T *isn;
1018
1019 RETURN_OK_IF_SKIP(cctx);
1020
1021 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1022 return FAIL;
1023 isn->isn_arg.checklen.cl_min_len = min_len;
1024 isn->isn_arg.checklen.cl_more_OK = more_OK;
1025
1026 return OK;
1027}
1028
1029/*
1030 * Generate an ISN_STORE instruction.
1031 */
1032 int
1033generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1034{
1035 isn_T *isn;
1036
1037 RETURN_OK_IF_SKIP(cctx);
1038 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1039 return FAIL;
1040 if (name != NULL)
1041 isn->isn_arg.string = vim_strsave(name);
1042 else
1043 isn->isn_arg.number = idx;
1044
1045 return OK;
1046}
1047
1048/*
Bram Moolenaard505d172022-12-18 21:42:55 +00001049 * Generate an ISN_LOAD_CLASSMEMBER ("load" == TRUE) or ISN_STORE_CLASSMEMBER
1050 * ("load" == FALSE) instruction.
1051 */
1052 int
1053generate_CLASSMEMBER(
1054 cctx_T *cctx,
1055 int load,
1056 class_T *cl,
1057 int idx)
1058{
1059 isn_T *isn;
1060
1061 RETURN_OK_IF_SKIP(cctx);
1062 if (load)
1063 {
1064 ocmember_T *m = &cl->class_class_members[idx];
1065 isn = generate_instr_type(cctx, ISN_LOAD_CLASSMEMBER, m->ocm_type);
1066 }
1067 else
1068 {
1069 isn = generate_instr_drop(cctx, ISN_STORE_CLASSMEMBER, 1);
1070 }
1071 if (isn == NULL)
1072 return FAIL;
1073 isn->isn_arg.classmember.cm_class = cl;
1074 ++cl->class_refcount;
1075 isn->isn_arg.classmember.cm_idx = idx;
1076
1077 return OK;
1078}
1079
1080/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001081 * Generate an ISN_STOREOUTER instruction.
1082 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +00001083 static int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001084generate_STOREOUTER(cctx_T *cctx, int idx, int level, int loop_idx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001085{
1086 isn_T *isn;
1087
1088 RETURN_OK_IF_SKIP(cctx);
1089 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1090 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001091 if (level == 1 && loop_idx >= 0 && idx >= loop_idx)
1092 {
1093 // Store a variable defined in a loop. A copy will be made at the end
1094 // of the loop. TODO: how about deeper nesting?
1095 isn->isn_arg.outer.outer_idx = idx - loop_idx;
1096 isn->isn_arg.outer.outer_depth = OUTER_LOOP_DEPTH;
1097 }
1098 else
1099 {
1100 isn->isn_arg.outer.outer_idx = idx;
1101 isn->isn_arg.outer.outer_depth = level;
1102 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001103
1104 return OK;
1105}
1106
1107/*
1108 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1109 */
1110 int
1111generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1112{
1113 isn_T *isn;
1114
1115 RETURN_OK_IF_SKIP(cctx);
1116 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1117 return FAIL;
1118 isn->isn_arg.storenr.stnr_idx = idx;
1119 isn->isn_arg.storenr.stnr_val = value;
1120
1121 return OK;
1122}
1123
1124/*
1125 * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction
1126 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +00001127 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001128generate_STOREOPT(
1129 cctx_T *cctx,
1130 isntype_T isn_type,
1131 char_u *name,
1132 int opt_flags)
1133{
1134 isn_T *isn;
1135
1136 RETURN_OK_IF_SKIP(cctx);
1137 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1138 return FAIL;
1139 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1140 isn->isn_arg.storeopt.so_flags = opt_flags;
1141
1142 return OK;
1143}
1144
1145/*
1146 * Generate an ISN_LOAD or similar instruction.
1147 */
1148 int
1149generate_LOAD(
1150 cctx_T *cctx,
1151 isntype_T isn_type,
1152 int idx,
1153 char_u *name,
1154 type_T *type)
1155{
1156 isn_T *isn;
1157
1158 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001159 if ((isn = generate_instr_type2(cctx, isn_type, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001160 return FAIL;
1161 if (name != NULL)
1162 isn->isn_arg.string = vim_strsave(name);
1163 else
1164 isn->isn_arg.number = idx;
1165
1166 return OK;
1167}
1168
1169/*
1170 * Generate an ISN_LOADOUTER instruction
1171 */
1172 int
1173generate_LOADOUTER(
1174 cctx_T *cctx,
1175 int idx,
1176 int nesting,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001177 int loop_depth,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001178 int loop_idx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001179 type_T *type)
1180{
1181 isn_T *isn;
1182
1183 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001184 if ((isn = generate_instr_type2(cctx, ISN_LOADOUTER, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001185 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001186 if (nesting == 1 && loop_idx >= 0 && idx >= loop_idx)
1187 {
1188 // Load a variable defined in a loop. A copy will be made at the end
Bram Moolenaarcc341812022-09-19 15:54:34 +01001189 // of the loop.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001190 isn->isn_arg.outer.outer_idx = idx - loop_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001191 isn->isn_arg.outer.outer_depth = -loop_depth - 1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001192 }
1193 else
1194 {
1195 isn->isn_arg.outer.outer_idx = idx;
1196 isn->isn_arg.outer.outer_depth = nesting;
1197 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001198
1199 return OK;
1200}
1201
1202/*
1203 * Generate an ISN_LOADV instruction for v:var.
1204 */
1205 int
1206generate_LOADV(
1207 cctx_T *cctx,
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001208 char_u *name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001209{
1210 int di_flags;
1211 int vidx = find_vim_var(name, &di_flags);
1212 type_T *type;
1213
1214 RETURN_OK_IF_SKIP(cctx);
1215 if (vidx < 0)
1216 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001217 semsg(_(e_variable_not_found_str), name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001218 return FAIL;
1219 }
Bram Moolenaard787e402021-12-24 21:36:12 +00001220 type = get_vim_var_type(vidx, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001221 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
1222}
1223
1224/*
1225 * Generate an ISN_UNLET instruction.
1226 */
1227 int
1228generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
1229{
1230 isn_T *isn;
1231
1232 RETURN_OK_IF_SKIP(cctx);
1233 if ((isn = generate_instr(cctx, isn_type)) == NULL)
1234 return FAIL;
1235 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1236 isn->isn_arg.unlet.ul_forceit = forceit;
1237
1238 return OK;
1239}
1240
1241/*
1242 * Generate an ISN_LOCKCONST instruction.
1243 */
1244 int
1245generate_LOCKCONST(cctx_T *cctx)
1246{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001247 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01001248 if (generate_instr(cctx, ISN_LOCKCONST) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001249 return FAIL;
1250 return OK;
1251}
1252
1253/*
1254 * Generate an ISN_LOADS instruction.
1255 */
1256 int
1257generate_OLDSCRIPT(
1258 cctx_T *cctx,
1259 isntype_T isn_type,
1260 char_u *name,
1261 int sid,
1262 type_T *type)
1263{
1264 isn_T *isn;
1265
1266 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001267 if (isn_type == ISN_LOADS || isn_type == ISN_LOADEXPORT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001268 isn = generate_instr_type(cctx, isn_type, type);
1269 else
1270 isn = generate_instr_drop(cctx, isn_type, 1);
1271 if (isn == NULL)
1272 return FAIL;
1273 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1274 isn->isn_arg.loadstore.ls_sid = sid;
1275
1276 return OK;
1277}
1278
1279/*
1280 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1281 */
1282 int
1283generate_VIM9SCRIPT(
1284 cctx_T *cctx,
1285 isntype_T isn_type,
1286 int sid,
1287 int idx,
1288 type_T *type)
1289{
1290 isn_T *isn;
1291 scriptref_T *sref;
1292 scriptitem_T *si = SCRIPT_ITEM(sid);
1293
1294 RETURN_OK_IF_SKIP(cctx);
1295 if (isn_type == ISN_LOADSCRIPT)
Bram Moolenaar160afdb2022-02-06 17:17:02 +00001296 isn = generate_instr_type2(cctx, isn_type, type, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001297 else
1298 isn = generate_instr_drop(cctx, isn_type, 1);
1299 if (isn == NULL)
1300 return FAIL;
1301
1302 // This requires three arguments, which doesn't fit in an instruction, thus
1303 // we need to allocate a struct for this.
1304 sref = ALLOC_ONE(scriptref_T);
1305 if (sref == NULL)
1306 return FAIL;
1307 isn->isn_arg.script.scriptref = sref;
1308 sref->sref_sid = sid;
1309 sref->sref_idx = idx;
1310 sref->sref_seq = si->sn_script_seq;
1311 sref->sref_type = type;
1312 return OK;
1313}
1314
1315/*
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001316 * Generate an ISN_NEWLIST instruction for "count" items.
1317 * "use_null" is TRUE for null_list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001318 */
1319 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001320generate_NEWLIST(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001321{
1322 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001323 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001324 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001325 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001326
1327 RETURN_OK_IF_SKIP(cctx);
1328 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1329 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001330 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001331
Bram Moolenaar078a4612022-01-04 15:17:03 +00001332 // Get the member type and the declared member type from all the items on
1333 // the stack.
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001334 member_type = get_member_type_from_stack(count, 1, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001335 type = get_list_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001336 decl_type = get_list_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001337
1338 // drop the value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001339 cctx->ctx_type_stack.ga_len -= count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001340
1341 // add the list type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001342 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001343}
1344
1345/*
1346 * Generate an ISN_NEWDICT instruction.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001347 * "use_null" is TRUE for null_dict.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001348 */
1349 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001350generate_NEWDICT(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001351{
1352 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001353 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001354 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001355 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001356
1357 RETURN_OK_IF_SKIP(cctx);
1358 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1359 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001360 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001361
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001362 member_type = get_member_type_from_stack(count, 2, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001363 type = get_dict_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001364 decl_type = get_dict_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001365
1366 // drop the key and value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001367 cctx->ctx_type_stack.ga_len -= 2 * count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001368
1369 // add the dict type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001370 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001371}
1372
1373/*
1374 * Generate an ISN_FUNCREF instruction.
Bram Moolenaar313e4722023-02-08 20:55:27 +00001375 * For "obj.Method" "cl" is the class of the object (can be an interface or a
1376 * base class) and "fi" the index of the method on that class.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001377 * "isnp" is set to the instruction, so that fr_dfunc_idx can be set later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001378 */
1379 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001380generate_FUNCREF(
1381 cctx_T *cctx,
1382 ufunc_T *ufunc,
Bram Moolenaar313e4722023-02-08 20:55:27 +00001383 class_T *cl,
1384 int fi,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001385 isn_T **isnp)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001386{
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001387 isn_T *isn;
1388 type_T *type;
1389 funcref_extra_T *extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001390 loopvarinfo_T loopinfo;
1391 int has_vars;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001392
1393 RETURN_OK_IF_SKIP(cctx);
1394 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1395 return FAIL;
Bram Moolenaara915fa02022-03-23 11:29:15 +00001396 if (isnp != NULL)
1397 *isnp = isn;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001398
Bram Moolenaarcc341812022-09-19 15:54:34 +01001399 has_vars = get_loop_var_info(cctx, &loopinfo);
Bram Moolenaar313e4722023-02-08 20:55:27 +00001400 if (ufunc->uf_def_status == UF_NOT_COMPILED || has_vars || cl != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001401 {
1402 extra = ALLOC_CLEAR_ONE(funcref_extra_T);
1403 if (extra == NULL)
1404 return FAIL;
1405 isn->isn_arg.funcref.fr_extra = extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001406 extra->fre_loopvar_info = loopinfo;
Bram Moolenaar313e4722023-02-08 20:55:27 +00001407 if (cl != NULL)
1408 {
1409 extra->fre_class = cl;
1410 ++cl->class_refcount;
1411 extra->fre_method_idx = fi;
1412 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001413 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00001414 if (ufunc->uf_def_status == UF_NOT_COMPILED || cl != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001415 extra->fre_func_name = vim_strsave(ufunc->uf_name);
Bram Moolenaar313e4722023-02-08 20:55:27 +00001416 if (ufunc->uf_def_status != UF_NOT_COMPILED && cl == NULL)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001417 {
1418 if (isnp == NULL && ufunc->uf_def_status == UF_TO_BE_COMPILED)
1419 // compile the function now, we need the uf_dfunc_idx value
1420 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001421 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001422 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01001423
1424 // Reserve an extra variable to keep track of the number of closures
1425 // created.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001426 cctx->ctx_has_closure = 1;
1427
1428 // If the referenced function is a closure, it may use items further up in
Bram Moolenaar139575d2022-03-15 19:29:30 +00001429 // the nested context, including this one. But not a function defined at
1430 // the script level.
1431 if ((ufunc->uf_flags & FC_CLOSURE)
1432 && func_name_refcount(cctx->ctx_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001433 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1434
Bram Moolenaar078a4612022-01-04 15:17:03 +00001435 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1436 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001437}
1438
1439/*
1440 * Generate an ISN_NEWFUNC instruction.
1441 * "lambda_name" and "func_name" must be in allocated memory and will be
1442 * consumed.
1443 */
1444 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001445generate_NEWFUNC(
1446 cctx_T *cctx,
1447 char_u *lambda_name,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001448 char_u *func_name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001449{
1450 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001451 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001452
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001453 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001454 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001455 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1456 ret = FAIL;
1457 else
1458 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001459 newfuncarg_T *arg = ALLOC_CLEAR_ONE(newfuncarg_T);
1460
1461 if (arg == NULL)
1462 ret = FAIL;
1463 else
1464 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01001465 // Reserve an extra variable to keep track of the number of
1466 // closures created.
1467 cctx->ctx_has_closure = 1;
1468
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001469 isn->isn_arg.newfunc.nf_arg = arg;
1470 arg->nfa_lambda = lambda_name;
1471 arg->nfa_global = func_name;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001472 (void)get_loop_var_info(cctx, &arg->nfa_loopvar_info);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001473 return OK;
1474 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001475 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001476 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001477 vim_free(lambda_name);
1478 vim_free(func_name);
1479 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001480}
1481
1482/*
1483 * Generate an ISN_DEF instruction: list functions
1484 */
1485 int
1486generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1487{
1488 isn_T *isn;
1489
1490 RETURN_OK_IF_SKIP(cctx);
1491 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1492 return FAIL;
1493 if (len > 0)
1494 {
1495 isn->isn_arg.string = vim_strnsave(name, len);
1496 if (isn->isn_arg.string == NULL)
1497 return FAIL;
1498 }
1499 return OK;
1500}
1501
1502/*
1503 * Generate an ISN_JUMP instruction.
1504 */
1505 int
1506generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1507{
1508 isn_T *isn;
1509 garray_T *stack = &cctx->ctx_type_stack;
1510
1511 RETURN_OK_IF_SKIP(cctx);
1512 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1513 return FAIL;
1514 isn->isn_arg.jump.jump_when = when;
1515 isn->isn_arg.jump.jump_where = where;
1516
1517 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1518 --stack->ga_len;
1519
1520 return OK;
1521}
1522
1523/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001524 * Generate an ISN_WHILE instruction. Similar to ISN_JUMP for :while
1525 */
1526 int
1527generate_WHILE(cctx_T *cctx, int funcref_idx)
1528{
1529 isn_T *isn;
1530 garray_T *stack = &cctx->ctx_type_stack;
1531
1532 RETURN_OK_IF_SKIP(cctx);
1533 if ((isn = generate_instr(cctx, ISN_WHILE)) == NULL)
1534 return FAIL;
1535 isn->isn_arg.whileloop.while_funcref_idx = funcref_idx;
1536 isn->isn_arg.whileloop.while_end = 0; // filled in later
1537
1538 if (stack->ga_len > 0)
1539 --stack->ga_len;
1540
1541 return OK;
1542}
1543
1544/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001545 * Generate an ISN_JUMP_IF_ARG_SET or ISN_JUMP_IF_ARG_NOT_SET instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001546 */
1547 int
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001548generate_JUMP_IF_ARG(cctx_T *cctx, isntype_T isn_type, int arg_off)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001549{
1550 isn_T *isn;
1551
1552 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001553 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001554 return FAIL;
1555 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1556 // jump_where is set later
1557 return OK;
1558}
1559
1560 int
1561generate_FOR(cctx_T *cctx, int loop_idx)
1562{
1563 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001564
1565 RETURN_OK_IF_SKIP(cctx);
1566 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1567 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001568 isn->isn_arg.forloop.for_loop_idx = loop_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001569
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001570 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001571 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001572}
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001573
1574 int
Bram Moolenaarcc341812022-09-19 15:54:34 +01001575generate_ENDLOOP(cctx_T *cctx, loop_info_T *loop_info)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001576{
1577 isn_T *isn;
1578
1579 RETURN_OK_IF_SKIP(cctx);
1580 if ((isn = generate_instr(cctx, ISN_ENDLOOP)) == NULL)
1581 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001582 isn->isn_arg.endloop.end_depth = loop_info->li_depth;
1583 isn->isn_arg.endloop.end_funcref_idx = loop_info->li_funcref_idx;
1584 isn->isn_arg.endloop.end_var_idx = loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001585 isn->isn_arg.endloop.end_var_count =
Bram Moolenaarcc341812022-09-19 15:54:34 +01001586 cctx->ctx_locals.ga_len - loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001587 return OK;
1588}
1589
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001590/*
1591 * Generate an ISN_TRYCONT instruction.
1592 */
1593 int
1594generate_TRYCONT(cctx_T *cctx, int levels, int where)
1595{
1596 isn_T *isn;
1597
1598 RETURN_OK_IF_SKIP(cctx);
1599 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1600 return FAIL;
1601 isn->isn_arg.trycont.tct_levels = levels;
1602 isn->isn_arg.trycont.tct_where = where;
1603
1604 return OK;
1605}
1606
Bram Moolenaar16900322022-09-08 19:51:45 +01001607/*
1608 * Check "argount" arguments and their types on the type stack.
1609 * Give an error and return FAIL if something is wrong.
1610 * When "method_call" is NULL no code is generated.
1611 */
1612 int
1613check_internal_func_args(
1614 cctx_T *cctx,
1615 int func_idx,
1616 int argcount,
1617 int method_call,
1618 type2_T **argtypes,
1619 type2_T *shuffled_argtypes)
1620{
1621 garray_T *stack = &cctx->ctx_type_stack;
1622 int argoff = check_internal_func(func_idx, argcount);
1623
1624 if (argoff < 0)
1625 return FAIL;
1626
1627 if (method_call && argoff > 1)
1628 {
Bram Moolenaarb7f22702023-04-27 16:24:07 +01001629 if (argcount < argoff)
1630 {
1631 semsg(_(e_not_enough_arguments_for_function_str),
1632 internal_func_name(func_idx));
1633 return FAIL;
1634 }
Bram Moolenaar16900322022-09-08 19:51:45 +01001635
Bram Moolenaarb7f22702023-04-27 16:24:07 +01001636 isn_T *isn = generate_instr(cctx, ISN_SHUFFLE);
Bram Moolenaar16900322022-09-08 19:51:45 +01001637 if (isn == NULL)
1638 return FAIL;
1639 isn->isn_arg.shuffle.shfl_item = argcount;
1640 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1641 }
1642
1643 if (argcount > 0)
1644 {
1645 type2_T *typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
1646
1647 // Check the types of the arguments.
1648 if (method_call && argoff > 1)
1649 {
1650 int i;
1651
1652 for (i = 0; i < argcount; ++i)
1653 shuffled_argtypes[i] = (i < argoff - 1)
1654 ? typep[i + 1]
1655 : (i == argoff - 1) ? typep[0] : typep[i];
1656 *argtypes = shuffled_argtypes;
1657 }
1658 else
1659 {
1660 int i;
1661
1662 for (i = 0; i < argcount; ++i)
1663 shuffled_argtypes[i] = typep[i];
1664 *argtypes = shuffled_argtypes;
1665 }
1666 if (internal_func_check_arg_types(*argtypes, func_idx, argcount,
1667 cctx) == FAIL)
1668 return FAIL;
1669 }
1670 return OK;
1671}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001672
1673/*
1674 * Generate an ISN_BCALL instruction.
1675 * "method_call" is TRUE for "value->method()"
1676 * Return FAIL if the number of arguments is wrong.
1677 */
1678 int
1679generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1680{
1681 isn_T *isn;
1682 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001683 type2_T *argtypes = NULL;
1684 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1685 type2_T *maptype = NULL;
1686 type_T *type;
Bram Moolenaar81330182022-02-01 12:11:58 +00001687 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001688
1689 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar16900322022-09-08 19:51:45 +01001690
1691 if (check_internal_func_args(cctx, func_idx, argcount, method_call,
1692 &argtypes, shuffled_argtypes) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001693 return FAIL;
1694
Bram Moolenaar16900322022-09-08 19:51:45 +01001695 if (internal_func_is_map(func_idx))
1696 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001697
1698 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1699 return FAIL;
1700 isn->isn_arg.bfunc.cbf_idx = func_idx;
1701 isn->isn_arg.bfunc.cbf_argcount = argcount;
1702
1703 // Drop the argument types and push the return type.
1704 stack->ga_len -= argcount;
Bram Moolenaar32517c42023-01-15 18:17:12 +00001705 type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type,
1706 cctx->ctx_type_list);
Bram Moolenaar81330182022-02-01 12:11:58 +00001707 if (push_type_stack2(cctx, type, decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001708 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001709
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001710 if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
1711 && maptype[0].type_decl->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001712 // Check that map() didn't change the item types.
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001713 generate_TYPECHECK(cctx, maptype[0].type_decl, FALSE, -1, FALSE, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001714
1715 return OK;
1716}
1717
1718/*
1719 * Generate an ISN_LISTAPPEND instruction. Works like add().
1720 * Argument count is already checked.
1721 */
1722 int
1723generate_LISTAPPEND(cctx_T *cctx)
1724{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001725 type_T *list_type;
1726 type_T *item_type;
1727 type_T *expected;
1728
1729 // Caller already checked that list_type is a list.
Bram Moolenaar81330182022-02-01 12:11:58 +00001730 // For checking the item type we use the declared type of the list and the
1731 // current type of the added item, adding a string to [1, 2] is OK.
1732 list_type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaarfa103972022-09-29 19:14:42 +01001733 if (arg_type_modifiable(list_type, 1) == FAIL)
1734 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001735 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001736 expected = list_type->tt_member;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001737 if (need_type(item_type, expected, FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001738 return FAIL;
1739
1740 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1741 return FAIL;
1742
Bram Moolenaar078a4612022-01-04 15:17:03 +00001743 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001744 return OK;
1745}
1746
1747/*
1748 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1749 * Argument count is already checked.
1750 */
1751 int
1752generate_BLOBAPPEND(cctx_T *cctx)
1753{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001754 type_T *item_type;
1755
Bram Moolenaarfa103972022-09-29 19:14:42 +01001756 // Caller already checked that blob_type is a blob, check it is modifiable.
1757 if (arg_type_modifiable(get_decl_type_on_stack(cctx, 1), 1) == FAIL)
1758 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001759 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001760 if (need_type(item_type, &t_number, FALSE,
1761 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001762 return FAIL;
1763
1764 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1765 return FAIL;
1766
Bram Moolenaar078a4612022-01-04 15:17:03 +00001767 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001768 return OK;
1769}
1770
1771/*
Bram Moolenaard0200c82023-01-28 15:19:40 +00001772 * Generate an ISN_DCALL, ISN_UCALL or ISN_METHODCALL instruction.
1773 * When calling a method on an object, of which we know the interface only,
1774 * then "cl" is the interface and "mi" the method index on the interface.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001775 * Return FAIL if the number of arguments is wrong.
1776 */
1777 int
Bram Moolenaard0200c82023-01-28 15:19:40 +00001778generate_CALL(
1779 cctx_T *cctx,
1780 ufunc_T *ufunc,
1781 class_T *cl,
1782 int mi,
h-east2261c892023-08-16 21:49:54 +09001783 type_T *mtype, // method type
Bram Moolenaard0200c82023-01-28 15:19:40 +00001784 int pushed_argcount)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001785{
1786 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001787 int regular_args = ufunc->uf_args.ga_len;
1788 int argcount = pushed_argcount;
1789
1790 RETURN_OK_IF_SKIP(cctx);
1791 if (argcount > regular_args && !has_varargs(ufunc))
1792 {
1793 semsg(_(e_too_many_arguments_for_function_str),
1794 printable_func_name(ufunc));
1795 return FAIL;
1796 }
1797 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1798 {
1799 semsg(_(e_not_enough_arguments_for_function_str),
1800 printable_func_name(ufunc));
1801 return FAIL;
1802 }
1803
1804 if (ufunc->uf_def_status != UF_NOT_COMPILED
1805 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1806 {
1807 int i;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001808 compiletype_T compile_type;
h-east2261c892023-08-16 21:49:54 +09001809 int class_constructor = (mtype->tt_type == VAR_CLASS
1810 && STRNCMP(ufunc->uf_name, "new", 3) == 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001811
1812 for (i = 0; i < argcount; ++i)
1813 {
1814 type_T *expected;
1815 type_T *actual;
1816
Bram Moolenaar078a4612022-01-04 15:17:03 +00001817 actual = get_type_on_stack(cctx, argcount - i - 1);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001818 if (actual->tt_type == VAR_SPECIAL
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001819 && i >= regular_args - ufunc->uf_def_args.ga_len)
1820 {
1821 // assume v:none used for default argument value
1822 continue;
1823 }
1824 if (i < regular_args)
1825 {
1826 if (ufunc->uf_arg_types == NULL)
1827 continue;
1828 expected = ufunc->uf_arg_types[i];
h-east2261c892023-08-16 21:49:54 +09001829
1830 // When the method is a class constructor and the formal
1831 // argument is an object member, the type check is performed on
1832 // the object member type.
1833 if (class_constructor && expected->tt_type == VAR_ANY)
1834 {
1835 class_T *clp = mtype->tt_class;
1836 char_u *aname = ((char_u **)ufunc->uf_args.ga_data)[i];
1837 for (int om = 0; om < clp->class_obj_member_count; ++om)
1838 {
1839 if (STRCMP(aname, clp->class_obj_members[om].ocm_name)
1840 == 0)
1841 {
1842 expected = clp->class_obj_members[om].ocm_type;
1843 break;
1844 }
1845 }
1846
1847 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001848 }
1849 else if (ufunc->uf_va_type == NULL
1850 || ufunc->uf_va_type == &t_list_any)
1851 // possibly a lambda or "...: any"
1852 expected = &t_any;
1853 else
1854 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001855 if (need_type(actual, expected, FALSE,
1856 -argcount + i, i + 1, cctx, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001857 {
1858 arg_type_mismatch(expected, actual, i + 1);
1859 return FAIL;
1860 }
1861 }
Bram Moolenaar139575d2022-03-15 19:29:30 +00001862 compile_type = get_compile_type(ufunc);
1863 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001864 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaar139575d2022-03-15 19:29:30 +00001865 compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001866 return FAIL;
1867 }
1868 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1869 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001870 emsg_funcname(e_call_to_function_that_failed_to_compile_str,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001871 ufunc->uf_name);
1872 return FAIL;
1873 }
1874
Bram Moolenaard0200c82023-01-28 15:19:40 +00001875 if ((isn = generate_instr(cctx, cl != NULL ? ISN_METHODCALL
1876 : ufunc->uf_def_status != UF_NOT_COMPILED
1877 ? ISN_DCALL : ISN_UCALL)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001878 return FAIL;
Bram Moolenaar836137d2023-01-29 14:11:24 +00001879 if (cl != NULL /* isn->isn_type == ISN_METHODCALL */)
Bram Moolenaard0200c82023-01-28 15:19:40 +00001880 {
1881 isn->isn_arg.mfunc = ALLOC_ONE(cmfunc_T);
1882 if (isn->isn_arg.mfunc == NULL)
1883 return FAIL;
1884 isn->isn_arg.mfunc->cmf_itf = cl;
1885 ++cl->class_refcount;
1886 isn->isn_arg.mfunc->cmf_idx = mi;
1887 isn->isn_arg.mfunc->cmf_argcount = argcount;
1888 }
1889 else if (isn->isn_type == ISN_DCALL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001890 {
1891 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1892 isn->isn_arg.dfunc.cdf_argcount = argcount;
1893 }
1894 else
1895 {
1896 // A user function may be deleted and redefined later, can't use the
1897 // ufunc pointer, need to look it up again at runtime.
1898 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1899 isn->isn_arg.ufunc.cuf_argcount = argcount;
1900 }
1901
Bram Moolenaar078a4612022-01-04 15:17:03 +00001902 // drop the argument types
1903 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001904
Bram Moolenaar078a4612022-01-04 15:17:03 +00001905 // add return type
1906 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001907}
1908
1909/*
1910 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1911 */
1912 int
1913generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1914{
1915 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001916
1917 RETURN_OK_IF_SKIP(cctx);
1918 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1919 return FAIL;
1920 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1921 isn->isn_arg.ufunc.cuf_argcount = argcount;
1922
Bram Moolenaar078a4612022-01-04 15:17:03 +00001923 // drop the argument types
1924 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001925
Bram Moolenaar078a4612022-01-04 15:17:03 +00001926 // add return value
1927 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001928}
1929
1930/*
Bram Moolenaar16900322022-09-08 19:51:45 +01001931 * Check the arguments of function "type" against the types on the stack.
1932 * Returns OK or FAIL;
1933 */
1934 int
1935check_func_args_from_type(
1936 cctx_T *cctx,
1937 type_T *type,
1938 int argcount,
1939 int at_top,
1940 char_u *name)
1941{
1942 if (type->tt_argcount != -1)
1943 {
1944 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1945
1946 if (argcount < type->tt_min_argcount - varargs)
1947 {
1948 emsg_funcname(e_not_enough_arguments_for_function_str, name);
1949 return FAIL;
1950 }
1951 if (!varargs && argcount > type->tt_argcount)
1952 {
1953 emsg_funcname(e_too_many_arguments_for_function_str, name);
1954 return FAIL;
1955 }
1956 if (type->tt_args != NULL)
1957 {
1958 int i;
1959
1960 for (i = 0; i < argcount; ++i)
1961 {
1962 int offset = -argcount + i - (at_top ? 0 : 1);
1963 type_T *actual = get_type_on_stack(cctx, -1 - offset);
1964 type_T *expected;
1965
1966 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaar36818a92023-01-03 12:33:26 +00001967 {
1968 expected = type->tt_args[type->tt_argcount - 1];
1969 if (expected != NULL && expected->tt_type == VAR_LIST)
1970 expected = expected->tt_member;
1971 if (expected == NULL)
1972 expected = &t_any;
1973 }
Bram Moolenaar16900322022-09-08 19:51:45 +01001974 else if (i >= type->tt_min_argcount
1975 && actual->tt_type == VAR_SPECIAL)
1976 expected = &t_any;
1977 else
1978 expected = type->tt_args[i];
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001979 if (need_type(actual, expected, FALSE,
1980 offset, i + 1, cctx, TRUE, FALSE) == FAIL)
Bram Moolenaar16900322022-09-08 19:51:45 +01001981 {
1982 arg_type_mismatch(expected, actual, i + 1);
1983 return FAIL;
1984 }
1985 }
1986 }
1987 }
1988
1989 return OK;
1990}
1991/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001992 * Generate an ISN_PCALL instruction.
1993 * "type" is the type of the FuncRef.
1994 */
1995 int
1996generate_PCALL(
1997 cctx_T *cctx,
1998 int argcount,
1999 char_u *name,
2000 type_T *type,
2001 int at_top)
2002{
2003 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002004 type_T *ret_type;
2005
2006 RETURN_OK_IF_SKIP(cctx);
2007
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002008 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002009 ret_type = &t_any;
2010 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
2011 {
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002012 if (check_func_args_from_type(cctx, type, argcount, at_top, name)
2013 == FAIL)
Bram Moolenaar16900322022-09-08 19:51:45 +01002014 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002015
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002016 ret_type = type->tt_member;
2017 if (ret_type == &t_unknown)
2018 // return type not known yet, use a runtime check
2019 ret_type = &t_any;
2020 }
2021 else
2022 {
2023 semsg(_(e_not_callable_type_str), name);
2024 return FAIL;
2025 }
2026
2027 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2028 return FAIL;
2029 isn->isn_arg.pfunc.cpf_top = at_top;
2030 isn->isn_arg.pfunc.cpf_argcount = argcount;
2031
Bram Moolenaar078a4612022-01-04 15:17:03 +00002032 // drop the arguments and the funcref/partial
2033 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002034
Bram Moolenaar078a4612022-01-04 15:17:03 +00002035 // push the return value
2036 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002037
2038 // If partial is above the arguments it must be cleared and replaced with
2039 // the return value.
2040 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2041 return FAIL;
2042
2043 return OK;
2044}
2045
2046/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002047 * Generate an ISN_DEFER instruction.
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00002048 * "obj_method" is one for "obj.Method()", zero otherwise.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002049 */
2050 int
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00002051generate_DEFER(cctx_T *cctx, int var_idx, int obj_method, int argcount)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002052{
2053 isn_T *isn;
2054
2055 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00002056 if ((isn = generate_instr_drop(cctx,
2057 obj_method == 0 ? ISN_DEFER : ISN_DEFEROBJ,
2058 argcount + 1)) == NULL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002059 return FAIL;
2060 isn->isn_arg.defer.defer_var_idx = var_idx;
2061 isn->isn_arg.defer.defer_argcount = argcount;
2062 return OK;
2063}
2064
2065/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002066 * Generate an ISN_STRINGMEMBER instruction.
2067 */
2068 int
2069generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
2070{
2071 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002072 type_T *type;
2073
2074 RETURN_OK_IF_SKIP(cctx);
2075 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
2076 return FAIL;
2077 isn->isn_arg.string = vim_strnsave(name, len);
2078
2079 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00002080 type = get_type_on_stack(cctx, 0);
Bram Moolenaar0089ce22022-10-08 14:39:36 +01002081 if (type->tt_type != VAR_DICT
2082 && type->tt_type != VAR_ANY && type->tt_type != VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002083 {
2084 char *tofree;
2085
2086 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
2087 name, type_name(type, &tofree));
2088 vim_free(tofree);
2089 return FAIL;
2090 }
2091 // change dict type to dict member type
2092 if (type->tt_type == VAR_DICT)
2093 {
Bram Moolenaar0089ce22022-10-08 14:39:36 +01002094 type_T *ntype = type->tt_member->tt_type == VAR_UNKNOWN
Bram Moolenaar078a4612022-01-04 15:17:03 +00002095 ? &t_any : type->tt_member;
2096 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002097 }
2098
2099 return OK;
2100}
2101
2102/*
2103 * Generate an ISN_ECHO instruction.
2104 */
2105 int
2106generate_ECHO(cctx_T *cctx, int with_white, int count)
2107{
2108 isn_T *isn;
2109
2110 RETURN_OK_IF_SKIP(cctx);
2111 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2112 return FAIL;
2113 isn->isn_arg.echo.echo_with_white = with_white;
2114 isn->isn_arg.echo.echo_count = count;
2115
2116 return OK;
2117}
2118
2119/*
2120 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
2121 */
2122 int
2123generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
2124{
2125 isn_T *isn;
2126
Bram Moolenaar2eae3d22022-10-07 15:09:27 +01002127 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002128 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
2129 return FAIL;
2130 isn->isn_arg.number = count;
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002131 return OK;
2132}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002133
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002134/*
2135 * Generate an ISN_ECHOWINDOW instruction
2136 */
2137 int
2138generate_ECHOWINDOW(cctx_T *cctx, int count, long time)
2139{
2140 isn_T *isn;
2141
2142 if ((isn = generate_instr_drop(cctx, ISN_ECHOWINDOW, count)) == NULL)
2143 return FAIL;
2144 isn->isn_arg.echowin.ewin_count = count;
2145 isn->isn_arg.echowin.ewin_time = time;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002146 return OK;
2147}
2148
2149/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002150 * Generate an ISN_SOURCE instruction.
2151 */
2152 int
2153generate_SOURCE(cctx_T *cctx, int sid)
2154{
2155 isn_T *isn;
2156
2157 if ((isn = generate_instr(cctx, ISN_SOURCE)) == NULL)
2158 return FAIL;
2159 isn->isn_arg.number = sid;
2160
2161 return OK;
2162}
2163
2164/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002165 * Generate an ISN_PUT instruction.
2166 */
2167 int
2168generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2169{
2170 isn_T *isn;
2171
2172 RETURN_OK_IF_SKIP(cctx);
2173 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2174 return FAIL;
2175 isn->isn_arg.put.put_regname = regname;
2176 isn->isn_arg.put.put_lnum = lnum;
2177 return OK;
2178}
2179
2180/*
2181 * Generate an EXEC instruction that takes a string argument.
2182 * A copy is made of "line".
2183 */
2184 int
2185generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
2186{
2187 isn_T *isn;
2188
2189 RETURN_OK_IF_SKIP(cctx);
2190 if ((isn = generate_instr(cctx, isntype)) == NULL)
2191 return FAIL;
2192 isn->isn_arg.string = vim_strsave(line);
2193 return OK;
2194}
2195
2196/*
2197 * Generate an EXEC instruction that takes a string argument.
2198 * "str" must be allocated, it is consumed.
2199 */
2200 int
2201generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
2202{
2203 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002204 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002205
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002206 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002207 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002208 if ((isn = generate_instr(cctx, isntype)) == NULL)
2209 ret = FAIL;
2210 else
2211 {
2212 isn->isn_arg.string = str;
2213 return OK;
2214 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002215 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002216 vim_free(str);
2217 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002218}
2219
2220 int
2221generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2222{
2223 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002224
2225 RETURN_OK_IF_SKIP(cctx);
2226 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2227 return FAIL;
2228 isn->isn_arg.string = vim_strsave(line);
2229
Bram Moolenaar078a4612022-01-04 15:17:03 +00002230 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002231}
2232
2233 int
2234generate_EXECCONCAT(cctx_T *cctx, int count)
2235{
2236 isn_T *isn;
2237
2238 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2239 return FAIL;
2240 isn->isn_arg.number = count;
2241 return OK;
2242}
2243
2244/*
2245 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2246 */
2247 int
2248generate_RANGE(cctx_T *cctx, char_u *range)
2249{
2250 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002251
2252 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2253 return FAIL;
2254 isn->isn_arg.string = range;
2255
Bram Moolenaar078a4612022-01-04 15:17:03 +00002256 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002257}
2258
2259 int
2260generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2261{
2262 isn_T *isn;
2263
2264 RETURN_OK_IF_SKIP(cctx);
2265 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2266 return FAIL;
2267 isn->isn_arg.unpack.unp_count = var_count;
2268 isn->isn_arg.unpack.unp_semicolon = semicolon;
2269 return OK;
2270}
2271
2272/*
2273 * Generate an instruction for any command modifiers.
2274 */
2275 int
2276generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
2277{
2278 isn_T *isn;
2279
2280 if (has_cmdmod(cmod, FALSE))
2281 {
2282 cctx->ctx_has_cmdmod = TRUE;
2283
2284 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
2285 return FAIL;
2286 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2287 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2288 return FAIL;
2289 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
2290 // filter program now belongs to the instruction
2291 cmod->cmod_filter_regmatch.regprog = NULL;
2292 }
2293
2294 return OK;
2295}
2296
2297 int
2298generate_undo_cmdmods(cctx_T *cctx)
2299{
2300 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2301 return FAIL;
2302 cctx->ctx_has_cmdmod = FALSE;
2303 return OK;
2304}
2305
2306/*
2307 * Generate a STORE instruction for "dest", not being "dest_local".
Bram Moolenaard505d172022-12-18 21:42:55 +00002308 * "lhs" might be NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002309 * Return FAIL when out of memory.
2310 */
2311 int
2312generate_store_var(
2313 cctx_T *cctx,
2314 assign_dest_T dest,
2315 int opt_flags,
2316 int vimvaridx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002317 type_T *type,
Bram Moolenaard505d172022-12-18 21:42:55 +00002318 char_u *name,
2319 lhs_T *lhs)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002320{
2321 switch (dest)
2322 {
2323 case dest_option:
2324 return generate_STOREOPT(cctx, ISN_STOREOPT,
2325 skip_option_env_lead(name), opt_flags);
2326 case dest_func_option:
2327 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
2328 skip_option_env_lead(name), opt_flags);
2329 case dest_global:
2330 // include g: with the name, easier to execute that way
2331 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
2332 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
2333 case dest_buffer:
2334 // include b: with the name, easier to execute that way
2335 return generate_STORE(cctx, ISN_STOREB, 0, name);
2336 case dest_window:
2337 // include w: with the name, easier to execute that way
2338 return generate_STORE(cctx, ISN_STOREW, 0, name);
2339 case dest_tab:
2340 // include t: with the name, easier to execute that way
2341 return generate_STORE(cctx, ISN_STORET, 0, name);
2342 case dest_env:
2343 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
2344 case dest_reg:
2345 return generate_STORE(cctx, ISN_STOREREG,
2346 name[1] == '@' ? '"' : name[1], NULL);
2347 case dest_vimvar:
2348 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
2349 case dest_script:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002350 {
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002351 int scriptvar_idx = lhs->lhs_scriptvar_idx;
2352 int scriptvar_sid = lhs->lhs_scriptvar_sid;
2353 if (scriptvar_idx < 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002354 {
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002355 isntype_T isn_type = ISN_STORES;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002356
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002357 if (SCRIPT_ID_VALID(scriptvar_sid)
2358 && SCRIPT_ITEM(scriptvar_sid)->sn_import_autoload
2359 && SCRIPT_ITEM(scriptvar_sid)->sn_autoload_prefix
2360 == NULL)
2361 {
2362 // "import autoload './dir/script.vim'" - load script
2363 // first
2364 if (generate_SOURCE(cctx, scriptvar_sid) == FAIL)
2365 return FAIL;
2366 isn_type = ISN_STOREEXPORT;
2367 }
2368
2369 // "s:" may be included in the name.
2370 return generate_OLDSCRIPT(cctx, isn_type, name,
2371 scriptvar_sid, type);
2372 }
2373 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002374 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002375 }
Bram Moolenaard505d172022-12-18 21:42:55 +00002376 case dest_class_member:
2377 return generate_CLASSMEMBER(cctx, FALSE,
2378 lhs->lhs_class, lhs->lhs_classmember_idx);
2379
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002380 case dest_local:
2381 case dest_expr:
2382 // cannot happen
2383 break;
2384 }
2385 return FAIL;
2386}
2387
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002388/*
2389 * Return TRUE when inside a "for" or "while" loop.
2390 */
2391 int
2392inside_loop_scope(cctx_T *cctx)
2393{
2394 scope_T *scope = cctx->ctx_scope;
2395
2396 for (;;)
2397 {
2398 if (scope == NULL)
2399 break;
2400 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
2401 return TRUE;
2402 scope = scope->se_outer;
2403 }
2404 return FALSE;
2405}
2406
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002407 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002408generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002409{
2410 if (lhs->lhs_dest != dest_local)
2411 return generate_store_var(cctx, lhs->lhs_dest,
2412 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
Bram Moolenaard505d172022-12-18 21:42:55 +00002413 lhs->lhs_type, lhs->lhs_name, lhs);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002414
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002415 if (lhs->lhs_lvar == NULL)
2416 return OK;
2417
2418 garray_T *instr = &cctx->ctx_instr;
2419 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2420
2421 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
2422 // ISN_STORENR.
2423 // And "var = 0" does not need any instruction.
2424 if (lhs->lhs_lvar->lv_from_outer == 0
2425 && instr->ga_len == instr_count + 1
2426 && isn->isn_type == ISN_PUSHNR)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002427 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002428 varnumber_T val = isn->isn_arg.number;
2429 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002430
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002431 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002432 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002433 // zero is the default value, no need to do anything
2434 --instr->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002435 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002436 else
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002437 {
2438 isn->isn_type = ISN_STORENR;
2439 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
2440 isn->isn_arg.storenr.stnr_val = val;
2441 }
2442 if (stack->ga_len > 0)
2443 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002444 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002445 else if (lhs->lhs_lvar->lv_from_outer > 0)
2446 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
2447 lhs->lhs_lvar->lv_from_outer, lhs->lhs_lvar->lv_loop_idx);
2448 else
2449 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002450 return OK;
2451}
2452
2453#if defined(FEAT_PROFILE) || defined(PROTO)
2454 void
2455may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2456{
2457 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
2458 generate_instr(cctx, ISN_PROF_END);
2459}
2460#endif
2461
2462
2463/*
2464 * Delete an instruction, free what it contains.
2465 */
2466 void
2467delete_instr(isn_T *isn)
2468{
2469 switch (isn->isn_type)
2470 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002471 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002472 case ISN_DEF:
2473 case ISN_EXEC:
2474 case ISN_EXECRANGE:
2475 case ISN_EXEC_SPLIT:
2476 case ISN_LEGACY_EVAL:
2477 case ISN_LOADAUTO:
2478 case ISN_LOADB:
2479 case ISN_LOADENV:
2480 case ISN_LOADG:
2481 case ISN_LOADOPT:
2482 case ISN_LOADT:
2483 case ISN_LOADW:
2484 case ISN_LOCKUNLOCK:
2485 case ISN_PUSHEXC:
2486 case ISN_PUSHFUNC:
2487 case ISN_PUSHS:
2488 case ISN_RANGE:
2489 case ISN_STOREAUTO:
2490 case ISN_STOREB:
2491 case ISN_STOREENV:
2492 case ISN_STOREG:
2493 case ISN_STORET:
2494 case ISN_STOREW:
2495 case ISN_STRINGMEMBER:
2496 vim_free(isn->isn_arg.string);
2497 break;
2498
2499 case ISN_SUBSTITUTE:
2500 {
2501 int idx;
2502 isn_T *list = isn->isn_arg.subs.subs_instr;
2503
2504 vim_free(isn->isn_arg.subs.subs_cmd);
2505 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2506 delete_instr(list + idx);
2507 vim_free(list);
2508 }
2509 break;
2510
2511 case ISN_INSTR:
2512 {
2513 int idx;
2514 isn_T *list = isn->isn_arg.instr;
2515
2516 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2517 delete_instr(list + idx);
2518 vim_free(list);
2519 }
2520 break;
2521
2522 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002523 case ISN_LOADEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002524 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002525 case ISN_STOREEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002526 vim_free(isn->isn_arg.loadstore.ls_name);
2527 break;
2528
2529 case ISN_UNLET:
2530 case ISN_UNLETENV:
2531 vim_free(isn->isn_arg.unlet.ul_name);
2532 break;
2533
2534 case ISN_STOREOPT:
2535 case ISN_STOREFUNCOPT:
2536 vim_free(isn->isn_arg.storeopt.so_name);
2537 break;
2538
2539 case ISN_PUSHBLOB: // push blob isn_arg.blob
2540 blob_unref(isn->isn_arg.blob);
2541 break;
2542
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002543 case ISN_PUSHCLASS:
Bram Moolenaar30a84472023-02-27 08:07:14 +00002544 class_unref(isn->isn_arg.classarg);
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002545 break;
2546
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002547 case ISN_UCALL:
2548 vim_free(isn->isn_arg.ufunc.cuf_name);
2549 break;
2550
2551 case ISN_FUNCREF:
2552 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002553 funcref_T *funcref = &isn->isn_arg.funcref;
2554 funcref_extra_T *extra = funcref->fr_extra;
2555
2556 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002557 {
2558 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002559 + funcref->fr_dfunc_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002560 ufunc_T *ufunc = dfunc->df_ufunc;
2561
2562 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2563 func_ptr_unref(ufunc);
2564 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002565 if (extra != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002566 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002567 char_u *name = extra->fre_func_name;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002568
2569 if (name != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002570 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002571 func_unref(name);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002572 vim_free(name);
2573 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00002574 if (extra->fre_class != NULL)
2575 class_unref(extra->fre_class);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002576 vim_free(extra);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002577 }
2578 }
2579 break;
2580
2581 case ISN_DCALL:
2582 {
2583 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002584 + isn->isn_arg.dfunc.cdf_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002585
2586 if (dfunc->df_ufunc != NULL
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002587 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002588 func_ptr_unref(dfunc->df_ufunc);
2589 }
2590 break;
2591
Bram Moolenaard0200c82023-01-28 15:19:40 +00002592 case ISN_METHODCALL:
2593 {
2594 cmfunc_T *mfunc = isn->isn_arg.mfunc;
2595 class_unref(mfunc->cmf_itf);
2596 vim_free(mfunc);
2597 }
2598 break;
2599
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002600 case ISN_NEWFUNC:
2601 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002602 newfuncarg_T *arg = isn->isn_arg.newfunc.nf_arg;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002603
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002604 if (arg != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002605 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002606 ufunc_T *ufunc = find_func_even_dead(
2607 arg->nfa_lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002608
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002609 if (ufunc != NULL)
2610 {
2611 unlink_def_function(ufunc);
2612 func_ptr_unref(ufunc);
2613 }
2614
2615 vim_free(arg->nfa_lambda);
2616 vim_free(arg->nfa_global);
2617 vim_free(arg);
2618 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002619 }
2620 break;
2621
2622 case ISN_CHECKTYPE:
2623 case ISN_SETTYPE:
2624 free_type(isn->isn_arg.type.ct_type);
2625 break;
2626
2627 case ISN_CMDMOD:
2628 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002629 ->cmod_filter_regmatch.regprog);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002630 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2631 break;
2632
2633 case ISN_LOADSCRIPT:
2634 case ISN_STORESCRIPT:
2635 vim_free(isn->isn_arg.script.scriptref);
2636 break;
2637
Bram Moolenaard505d172022-12-18 21:42:55 +00002638 case ISN_LOAD_CLASSMEMBER:
2639 case ISN_STORE_CLASSMEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00002640 case ISN_GET_ITF_MEMBER:
Bram Moolenaard505d172022-12-18 21:42:55 +00002641 class_unref(isn->isn_arg.classmember.cm_class);
2642 break;
2643
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002644 case ISN_STOREINDEX:
2645 class_unref(isn->isn_arg.storeindex.si_class);
2646 break;
2647
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002648 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002649 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002650 break;
2651
2652 case ISN_CEXPR_CORE:
2653 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2654 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2655 break;
2656
2657 case ISN_2BOOL:
2658 case ISN_2STRING:
2659 case ISN_2STRING_ANY:
2660 case ISN_ADDBLOB:
2661 case ISN_ADDLIST:
2662 case ISN_ANYINDEX:
2663 case ISN_ANYSLICE:
2664 case ISN_BCALL:
2665 case ISN_BLOBAPPEND:
2666 case ISN_BLOBINDEX:
2667 case ISN_BLOBSLICE:
2668 case ISN_CATCH:
2669 case ISN_CEXPR_AUCMD:
2670 case ISN_CHECKLEN:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002671 case ISN_CLEARDICT:
2672 case ISN_CMDMOD_REV:
2673 case ISN_COMPAREANY:
2674 case ISN_COMPAREBLOB:
2675 case ISN_COMPAREBOOL:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002676 case ISN_COMPARECLASS:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002677 case ISN_COMPAREDICT:
2678 case ISN_COMPAREFLOAT:
2679 case ISN_COMPAREFUNC:
2680 case ISN_COMPARELIST:
2681 case ISN_COMPARENR:
Bram Moolenaar7a222242022-03-01 19:23:24 +00002682 case ISN_COMPARENULL:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002683 case ISN_COMPAREOBJECT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002684 case ISN_COMPARESPECIAL:
2685 case ISN_COMPARESTRING:
2686 case ISN_CONCAT:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002687 case ISN_CONSTRUCT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002688 case ISN_COND2BOOL:
2689 case ISN_DEBUG:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002690 case ISN_DEFER:
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00002691 case ISN_DEFEROBJ:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002692 case ISN_DROP:
2693 case ISN_ECHO:
2694 case ISN_ECHOCONSOLE:
2695 case ISN_ECHOERR:
2696 case ISN_ECHOMSG:
Bram Moolenaar68a635a2022-09-01 17:26:17 +01002697 case ISN_ECHOWINDOW:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002698 case ISN_ENDLOOP:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002699 case ISN_ENDTRY:
2700 case ISN_EXECCONCAT:
2701 case ISN_EXECUTE:
2702 case ISN_FINALLY:
2703 case ISN_FINISH:
2704 case ISN_FOR:
2705 case ISN_GETITEM:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002706 case ISN_GET_OBJ_MEMBER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002707 case ISN_JUMP:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002708 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002709 case ISN_JUMP_IF_ARG_SET:
2710 case ISN_LISTAPPEND:
2711 case ISN_LISTINDEX:
2712 case ISN_LISTSLICE:
2713 case ISN_LOAD:
2714 case ISN_LOADBDICT:
2715 case ISN_LOADGDICT:
2716 case ISN_LOADOUTER:
2717 case ISN_LOADREG:
2718 case ISN_LOADTDICT:
2719 case ISN_LOADV:
2720 case ISN_LOADWDICT:
2721 case ISN_LOCKCONST:
2722 case ISN_MEMBER:
2723 case ISN_NEGATENR:
2724 case ISN_NEWDICT:
2725 case ISN_NEWLIST:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002726 case ISN_NEWPARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002727 case ISN_OPANY:
2728 case ISN_OPFLOAT:
2729 case ISN_OPNR:
2730 case ISN_PCALL:
2731 case ISN_PCALL_END:
2732 case ISN_PROF_END:
2733 case ISN_PROF_START:
2734 case ISN_PUSHBOOL:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002735 case ISN_PUSHCHANNEL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002736 case ISN_PUSHF:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002737 case ISN_PUSHJOB:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002738 case ISN_PUSHNR:
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002739 case ISN_PUSHOBJ:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002740 case ISN_PUSHSPEC:
2741 case ISN_PUT:
2742 case ISN_REDIREND:
2743 case ISN_REDIRSTART:
2744 case ISN_RETURN:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002745 case ISN_RETURN_OBJECT:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002746 case ISN_RETURN_VOID:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002747 case ISN_SHUFFLE:
2748 case ISN_SLICE:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002749 case ISN_SOURCE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002750 case ISN_STORE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002751 case ISN_STORENR:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002752 case ISN_STOREOUTER:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002753 case ISN_STORE_THIS:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002754 case ISN_STORERANGE:
2755 case ISN_STOREREG:
2756 case ISN_STOREV:
2757 case ISN_STRINDEX:
2758 case ISN_STRSLICE:
2759 case ISN_THROW:
2760 case ISN_TRYCONT:
2761 case ISN_UNLETINDEX:
2762 case ISN_UNLETRANGE:
2763 case ISN_UNPACK:
2764 case ISN_USEDICT:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002765 case ISN_WHILE:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002766 // nothing allocated
2767 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002768 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002769}
2770
2771 void
2772clear_instr_ga(garray_T *gap)
2773{
2774 int idx;
2775
2776 for (idx = 0; idx < gap->ga_len; ++idx)
2777 delete_instr(((isn_T *)gap->ga_data) + idx);
2778 ga_clear(gap);
2779}
2780
2781
2782#endif // defined(FEAT_EVAL)