blob: 4f228e4aa712ccf54019a5e5bc278c3d153b9d07 [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(
416 exprtype_T exprtype,
417 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
488 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000489 && (vartype1 == VAR_BOOL || vartype1 == VAR_SPECIAL
490 || vartype2 == VAR_BOOL || vartype2 == VAR_SPECIAL)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000491 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar7a222242022-03-01 19:23:24 +0000492 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000493 && (vartype1 == VAR_BLOB || vartype2 == VAR_BLOB
494 || vartype1 == VAR_LIST || vartype2 == VAR_LIST))))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000495 {
496 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000497 vartype_name(vartype1), vartype_name(vartype2));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000498 return ISN_DROP;
499 }
500 return isntype;
501}
502
503 int
504check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
505{
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000506 if (get_compare_isn(type, tv1, tv2, NULL, NULL) == ISN_DROP)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000507 return FAIL;
508 return OK;
509}
510
511/*
512 * Generate an ISN_COMPARE* instruction with a boolean result.
513 */
514 int
515generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
516{
517 isntype_T isntype;
518 isn_T *isn;
519 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000520
521 RETURN_OK_IF_SKIP(cctx);
522
523 // Get the known type of the two items on the stack. If they are matching
524 // use a type-specific instruction. Otherwise fall back to runtime type
525 // checking.
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000526 isntype = get_compare_isn(exprtype, NULL, NULL,
527 get_type_on_stack(cctx, 1), get_type_on_stack(cctx, 0));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000528 if (isntype == ISN_DROP)
529 return FAIL;
530
531 if ((isn = generate_instr(cctx, isntype)) == NULL)
532 return FAIL;
533 isn->isn_arg.op.op_type = exprtype;
534 isn->isn_arg.op.op_ic = ic;
535
536 // takes two arguments, puts one bool back
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100537 --stack->ga_len;
538 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000539
540 return OK;
541}
542
543/*
LemonBoy372bcce2022-04-25 12:43:20 +0100544 * Generate an ISN_CONCAT instruction.
545 * "count" is the number of stack elements to join together and it must be
546 * greater or equal to one.
547 * The caller ensures all the "count" elements on the stack have the right type.
548 */
549 int
550generate_CONCAT(cctx_T *cctx, int count)
551{
552 isn_T *isn;
553 garray_T *stack = &cctx->ctx_type_stack;
554
555 RETURN_OK_IF_SKIP(cctx);
556
LemonBoy372bcce2022-04-25 12:43:20 +0100557 if ((isn = generate_instr(cctx, ISN_CONCAT)) == NULL)
558 return FAIL;
559 isn->isn_arg.number = count;
560
561 // drop the argument types
562 stack->ga_len -= count - 1;
563
564 return OK;
565}
566
567/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000568 * Generate an ISN_2BOOL instruction.
569 * "offset" is the offset in the type stack.
570 */
571 int
572generate_2BOOL(cctx_T *cctx, int invert, int offset)
573{
574 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000575
576 RETURN_OK_IF_SKIP(cctx);
577 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
578 return FAIL;
579 isn->isn_arg.tobool.invert = invert;
580 isn->isn_arg.tobool.offset = offset;
581
582 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000583 set_type_on_stack(cctx, &t_bool, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000584
585 return OK;
586}
587
588/*
589 * Generate an ISN_COND2BOOL instruction.
590 */
591 int
592generate_COND2BOOL(cctx_T *cctx)
593{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000594 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100595 if (generate_instr(cctx, ISN_COND2BOOL) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000596 return FAIL;
597
598 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000599 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000600
601 return OK;
602}
603
604 int
605generate_TYPECHECK(
606 cctx_T *cctx,
607 type_T *expected,
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000608 int number_ok, // add TTFLAG_NUMBER_OK flag
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000609 int offset,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100610 int is_var,
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000611 int argidx)
612{
613 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000614
615 RETURN_OK_IF_SKIP(cctx);
616 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
617 return FAIL;
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000618 type_T *tt;
619 if (expected->tt_type == VAR_FLOAT && number_ok)
620 {
621 // always allocate, also for static types
622 tt = ALLOC_ONE(type_T);
623 if (tt != NULL)
624 {
625 *tt = *expected;
Bram Moolenaarc4b3f642022-12-30 10:36:34 +0000626 tt->tt_flags &= ~TTFLAG_STATIC;
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000627 tt->tt_flags |= TTFLAG_NUMBER_OK;
628 }
629 }
630 else
631 tt = alloc_type(expected);
632
633 isn->isn_arg.type.ct_type = tt;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000634 isn->isn_arg.type.ct_off = (int8_T)offset;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100635 isn->isn_arg.type.ct_is_var = is_var;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000636 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
637
638 // type becomes expected
Bram Moolenaar078a4612022-01-04 15:17:03 +0000639 set_type_on_stack(cctx, expected, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000640
641 return OK;
642}
643
644 int
645generate_SETTYPE(
646 cctx_T *cctx,
647 type_T *expected)
648{
649 isn_T *isn;
650
651 RETURN_OK_IF_SKIP(cctx);
652 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
653 return FAIL;
654 isn->isn_arg.type.ct_type = alloc_type(expected);
655 return OK;
656}
657
658/*
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000659 * Generate an ISN_PUSHOBJ instruction. Object is always NULL.
660 */
661 static int
662generate_PUSHOBJ(cctx_T *cctx)
663{
664 RETURN_OK_IF_SKIP(cctx);
665 if (generate_instr_type(cctx, ISN_PUSHOBJ, &t_any) == NULL)
666 return FAIL;
667 return OK;
668}
669
670/*
671 * Generate an ISN_PUSHCLASS instruction. "class" can be NULL.
672 */
673 static int
674generate_PUSHCLASS(cctx_T *cctx, class_T *class)
675{
676 RETURN_OK_IF_SKIP(cctx);
677 isn_T *isn = generate_instr_type(cctx, ISN_PUSHCLASS,
678 class == NULL ? &t_any : &class->class_type);
679 if (isn == NULL)
680 return FAIL;
681 isn->isn_arg.class = class;
682 if (class != NULL)
683 ++class->class_refcount;
684 return OK;
685}
686
687/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000688 * Generate a PUSH instruction for "tv".
689 * "tv" will be consumed or cleared.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000690 */
691 int
692generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
693{
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100694 switch (tv->v_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000695 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100696 case VAR_BOOL:
697 generate_PUSHBOOL(cctx, tv->vval.v_number);
698 break;
699 case VAR_SPECIAL:
700 generate_PUSHSPEC(cctx, tv->vval.v_number);
701 break;
702 case VAR_NUMBER:
703 generate_PUSHNR(cctx, tv->vval.v_number);
704 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100705 case VAR_FLOAT:
706 generate_PUSHF(cctx, tv->vval.v_float);
707 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100708 case VAR_BLOB:
709 generate_PUSHBLOB(cctx, tv->vval.v_blob);
710 tv->vval.v_blob = NULL;
711 break;
712 case VAR_LIST:
713 if (tv->vval.v_list != NULL)
714 iemsg("non-empty list constant not supported");
715 generate_NEWLIST(cctx, 0, TRUE);
716 break;
717 case VAR_DICT:
718 if (tv->vval.v_dict != NULL)
719 iemsg("non-empty dict constant not supported");
720 generate_NEWDICT(cctx, 0, TRUE);
721 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000722#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100723 case VAR_JOB:
724 if (tv->vval.v_job != NULL)
725 iemsg("non-null job constant not supported");
726 generate_PUSHJOB(cctx);
727 break;
728 case VAR_CHANNEL:
729 if (tv->vval.v_channel != NULL)
730 iemsg("non-null channel constant not supported");
731 generate_PUSHCHANNEL(cctx);
732 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000733#endif
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100734 case VAR_FUNC:
735 if (tv->vval.v_string != NULL)
736 iemsg("non-null function constant not supported");
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100737 generate_PUSHFUNC(cctx, NULL, &t_func_unknown, TRUE);
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100738 break;
739 case VAR_PARTIAL:
740 if (tv->vval.v_partial != NULL)
741 iemsg("non-null partial constant not supported");
742 if (generate_instr_type(cctx, ISN_NEWPARTIAL, &t_func_unknown)
743 == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000744 return FAIL;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100745 break;
746 case VAR_STRING:
747 generate_PUSHS(cctx, &tv->vval.v_string);
748 tv->vval.v_string = NULL;
749 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000750 case VAR_OBJECT:
751 if (tv->vval.v_object != NULL)
752 {
753 emsg(_(e_cannot_use_non_null_object));
754 return FAIL;
755 }
756 generate_PUSHOBJ(cctx);
757 break;
758 case VAR_CLASS:
759 generate_PUSHCLASS(cctx, tv->vval.v_class);
760 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100761 default:
762 siemsg("constant type %d not supported", tv->v_type);
763 clear_tv(tv);
764 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000765 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100766 tv->v_type = VAR_UNKNOWN;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000767 return OK;
768}
769
770/*
771 * Generate an ISN_PUSHNR instruction.
772 */
773 int
774generate_PUSHNR(cctx_T *cctx, varnumber_T number)
775{
776 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000777
778 RETURN_OK_IF_SKIP(cctx);
779 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
780 return FAIL;
781 isn->isn_arg.number = number;
782
783 if (number == 0 || number == 1)
784 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000785 set_type_on_stack(cctx, &t_number_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000786 return OK;
787}
788
789/*
790 * Generate an ISN_PUSHBOOL instruction.
791 */
792 int
793generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
794{
795 isn_T *isn;
796
797 RETURN_OK_IF_SKIP(cctx);
798 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
799 return FAIL;
800 isn->isn_arg.number = number;
801
802 return OK;
803}
804
805/*
806 * Generate an ISN_PUSHSPEC instruction.
807 */
808 int
809generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
810{
811 isn_T *isn;
812
813 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000814 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC,
815 number == VVAL_NULL ? &t_null : &t_none)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000816 return FAIL;
817 isn->isn_arg.number = number;
818
819 return OK;
820}
821
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000822/*
823 * Generate an ISN_PUSHF instruction.
824 */
825 int
826generate_PUSHF(cctx_T *cctx, float_T fnumber)
827{
828 isn_T *isn;
829
830 RETURN_OK_IF_SKIP(cctx);
831 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
832 return FAIL;
833 isn->isn_arg.fnumber = fnumber;
834
835 return OK;
836}
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000837
838/*
839 * Generate an ISN_PUSHS instruction.
840 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100841 * Note that if "str" is used in the instruction OK is returned and "*str" is
842 * not set to NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000843 */
844 int
845generate_PUSHS(cctx_T *cctx, char_u **str)
846{
847 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100848 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000849
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100850 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000851 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100852 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
853 ret = FAIL;
854 else
855 {
856 isn->isn_arg.string = str == NULL ? NULL : *str;
857 return OK;
858 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000859 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100860 if (str != NULL)
861 VIM_CLEAR(*str);
862 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000863}
864
865/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000866 * Generate an ISN_PUSHCHANNEL instruction. Channel is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000867 */
868 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000869generate_PUSHCHANNEL(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000870{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000871 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100872#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100873 if (generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000874 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000875 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100876#else
877 emsg(_(e_channel_job_feature_not_available));
878 return FAIL;
879#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000880}
881
882/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000883 * Generate an ISN_PUSHJOB instruction. Job is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000884 */
885 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000886generate_PUSHJOB(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000887{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000888 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100889#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100890 if (generate_instr_type(cctx, ISN_PUSHJOB, &t_job) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000891 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000892 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100893#else
894 emsg(_(e_channel_job_feature_not_available));
895 return FAIL;
896#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000897}
898
899/*
900 * Generate an ISN_PUSHBLOB instruction.
901 * Consumes "blob".
902 */
903 int
904generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
905{
906 isn_T *isn;
907
908 RETURN_OK_IF_SKIP(cctx);
909 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
910 return FAIL;
911 isn->isn_arg.blob = blob;
912
913 return OK;
914}
915
916/*
917 * Generate an ISN_PUSHFUNC instruction with name "name".
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100918 * When "may_prefix" is TRUE prefix "g:" unless "name" is script-local or
919 * autoload.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000920 */
921 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100922generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type, int may_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000923{
924 isn_T *isn;
925 char_u *funcname;
926
927 RETURN_OK_IF_SKIP(cctx);
928 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
929 return FAIL;
930 if (name == NULL)
931 funcname = NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100932 else if (!may_prefix
933 || *name == K_SPECIAL // script-local
Bram Moolenaard041f422022-01-12 19:54:00 +0000934 || vim_strchr(name, AUTOLOAD_CHAR) != NULL) // autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000935 funcname = vim_strsave(name);
936 else
937 {
938 funcname = alloc(STRLEN(name) + 3);
939 if (funcname != NULL)
940 {
941 STRCPY(funcname, "g:");
942 STRCPY(funcname + 2, name);
943 }
944 }
945
946 isn->isn_arg.string = funcname;
947 return OK;
948}
949
950/*
Bram Moolenaar06b77222022-01-25 15:51:56 +0000951 * Generate an ISN_AUTOLOAD instruction.
952 */
953 int
954generate_AUTOLOAD(cctx_T *cctx, char_u *name, type_T *type)
955{
956 isn_T *isn;
957
958 RETURN_OK_IF_SKIP(cctx);
959 if ((isn = generate_instr_type(cctx, ISN_AUTOLOAD, type)) == NULL)
960 return FAIL;
961 isn->isn_arg.string = vim_strsave(name);
962 if (isn->isn_arg.string == NULL)
963 return FAIL;
964 return OK;
965}
966
967/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000968 * Generate an ISN_GETITEM instruction with "index".
969 * "with_op" is TRUE for "+=" and other operators, the stack has the current
970 * value below the list with values.
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100971 * Caller must check the type is a list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000972 */
973 int
974generate_GETITEM(cctx_T *cctx, int index, int with_op)
975{
976 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000977 type_T *type = get_type_on_stack(cctx, with_op ? 1 : 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000978 type_T *item_type = &t_any;
979
980 RETURN_OK_IF_SKIP(cctx);
981
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000982 item_type = type->tt_member;
983 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
984 return FAIL;
985 isn->isn_arg.getitem.gi_index = index;
986 isn->isn_arg.getitem.gi_with_op = with_op;
987
988 // add the item type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +0000989 return push_type_stack(cctx, item_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000990}
991
992/*
993 * Generate an ISN_SLICE instruction with "count".
994 */
995 int
996generate_SLICE(cctx_T *cctx, int count)
997{
998 isn_T *isn;
999
1000 RETURN_OK_IF_SKIP(cctx);
1001 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1002 return FAIL;
1003 isn->isn_arg.number = count;
1004 return OK;
1005}
1006
1007/*
1008 * Generate an ISN_CHECKLEN instruction with "min_len".
1009 */
1010 int
1011generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1012{
1013 isn_T *isn;
1014
1015 RETURN_OK_IF_SKIP(cctx);
1016
1017 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1018 return FAIL;
1019 isn->isn_arg.checklen.cl_min_len = min_len;
1020 isn->isn_arg.checklen.cl_more_OK = more_OK;
1021
1022 return OK;
1023}
1024
1025/*
1026 * Generate an ISN_STORE instruction.
1027 */
1028 int
1029generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1030{
1031 isn_T *isn;
1032
1033 RETURN_OK_IF_SKIP(cctx);
1034 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1035 return FAIL;
1036 if (name != NULL)
1037 isn->isn_arg.string = vim_strsave(name);
1038 else
1039 isn->isn_arg.number = idx;
1040
1041 return OK;
1042}
1043
1044/*
Bram Moolenaard505d172022-12-18 21:42:55 +00001045 * Generate an ISN_LOAD_CLASSMEMBER ("load" == TRUE) or ISN_STORE_CLASSMEMBER
1046 * ("load" == FALSE) instruction.
1047 */
1048 int
1049generate_CLASSMEMBER(
1050 cctx_T *cctx,
1051 int load,
1052 class_T *cl,
1053 int idx)
1054{
1055 isn_T *isn;
1056
1057 RETURN_OK_IF_SKIP(cctx);
1058 if (load)
1059 {
1060 ocmember_T *m = &cl->class_class_members[idx];
1061 isn = generate_instr_type(cctx, ISN_LOAD_CLASSMEMBER, m->ocm_type);
1062 }
1063 else
1064 {
1065 isn = generate_instr_drop(cctx, ISN_STORE_CLASSMEMBER, 1);
1066 }
1067 if (isn == NULL)
1068 return FAIL;
1069 isn->isn_arg.classmember.cm_class = cl;
1070 ++cl->class_refcount;
1071 isn->isn_arg.classmember.cm_idx = idx;
1072
1073 return OK;
1074}
1075
1076/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001077 * Generate an ISN_STOREOUTER instruction.
1078 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +00001079 static int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001080generate_STOREOUTER(cctx_T *cctx, int idx, int level, int loop_idx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001081{
1082 isn_T *isn;
1083
1084 RETURN_OK_IF_SKIP(cctx);
1085 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1086 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001087 if (level == 1 && loop_idx >= 0 && idx >= loop_idx)
1088 {
1089 // Store a variable defined in a loop. A copy will be made at the end
1090 // of the loop. TODO: how about deeper nesting?
1091 isn->isn_arg.outer.outer_idx = idx - loop_idx;
1092 isn->isn_arg.outer.outer_depth = OUTER_LOOP_DEPTH;
1093 }
1094 else
1095 {
1096 isn->isn_arg.outer.outer_idx = idx;
1097 isn->isn_arg.outer.outer_depth = level;
1098 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001099
1100 return OK;
1101}
1102
1103/*
1104 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1105 */
1106 int
1107generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1108{
1109 isn_T *isn;
1110
1111 RETURN_OK_IF_SKIP(cctx);
1112 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1113 return FAIL;
1114 isn->isn_arg.storenr.stnr_idx = idx;
1115 isn->isn_arg.storenr.stnr_val = value;
1116
1117 return OK;
1118}
1119
1120/*
1121 * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction
1122 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +00001123 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001124generate_STOREOPT(
1125 cctx_T *cctx,
1126 isntype_T isn_type,
1127 char_u *name,
1128 int opt_flags)
1129{
1130 isn_T *isn;
1131
1132 RETURN_OK_IF_SKIP(cctx);
1133 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1134 return FAIL;
1135 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1136 isn->isn_arg.storeopt.so_flags = opt_flags;
1137
1138 return OK;
1139}
1140
1141/*
1142 * Generate an ISN_LOAD or similar instruction.
1143 */
1144 int
1145generate_LOAD(
1146 cctx_T *cctx,
1147 isntype_T isn_type,
1148 int idx,
1149 char_u *name,
1150 type_T *type)
1151{
1152 isn_T *isn;
1153
1154 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001155 if ((isn = generate_instr_type2(cctx, isn_type, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001156 return FAIL;
1157 if (name != NULL)
1158 isn->isn_arg.string = vim_strsave(name);
1159 else
1160 isn->isn_arg.number = idx;
1161
1162 return OK;
1163}
1164
1165/*
1166 * Generate an ISN_LOADOUTER instruction
1167 */
1168 int
1169generate_LOADOUTER(
1170 cctx_T *cctx,
1171 int idx,
1172 int nesting,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001173 int loop_depth,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001174 int loop_idx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001175 type_T *type)
1176{
1177 isn_T *isn;
1178
1179 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001180 if ((isn = generate_instr_type2(cctx, ISN_LOADOUTER, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001181 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001182 if (nesting == 1 && loop_idx >= 0 && idx >= loop_idx)
1183 {
1184 // Load a variable defined in a loop. A copy will be made at the end
Bram Moolenaarcc341812022-09-19 15:54:34 +01001185 // of the loop.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001186 isn->isn_arg.outer.outer_idx = idx - loop_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001187 isn->isn_arg.outer.outer_depth = -loop_depth - 1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001188 }
1189 else
1190 {
1191 isn->isn_arg.outer.outer_idx = idx;
1192 isn->isn_arg.outer.outer_depth = nesting;
1193 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001194
1195 return OK;
1196}
1197
1198/*
1199 * Generate an ISN_LOADV instruction for v:var.
1200 */
1201 int
1202generate_LOADV(
1203 cctx_T *cctx,
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001204 char_u *name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001205{
1206 int di_flags;
1207 int vidx = find_vim_var(name, &di_flags);
1208 type_T *type;
1209
1210 RETURN_OK_IF_SKIP(cctx);
1211 if (vidx < 0)
1212 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001213 semsg(_(e_variable_not_found_str), name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001214 return FAIL;
1215 }
Bram Moolenaard787e402021-12-24 21:36:12 +00001216 type = get_vim_var_type(vidx, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001217 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
1218}
1219
1220/*
1221 * Generate an ISN_UNLET instruction.
1222 */
1223 int
1224generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
1225{
1226 isn_T *isn;
1227
1228 RETURN_OK_IF_SKIP(cctx);
1229 if ((isn = generate_instr(cctx, isn_type)) == NULL)
1230 return FAIL;
1231 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1232 isn->isn_arg.unlet.ul_forceit = forceit;
1233
1234 return OK;
1235}
1236
1237/*
1238 * Generate an ISN_LOCKCONST instruction.
1239 */
1240 int
1241generate_LOCKCONST(cctx_T *cctx)
1242{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001243 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01001244 if (generate_instr(cctx, ISN_LOCKCONST) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001245 return FAIL;
1246 return OK;
1247}
1248
1249/*
1250 * Generate an ISN_LOADS instruction.
1251 */
1252 int
1253generate_OLDSCRIPT(
1254 cctx_T *cctx,
1255 isntype_T isn_type,
1256 char_u *name,
1257 int sid,
1258 type_T *type)
1259{
1260 isn_T *isn;
1261
1262 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001263 if (isn_type == ISN_LOADS || isn_type == ISN_LOADEXPORT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001264 isn = generate_instr_type(cctx, isn_type, type);
1265 else
1266 isn = generate_instr_drop(cctx, isn_type, 1);
1267 if (isn == NULL)
1268 return FAIL;
1269 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1270 isn->isn_arg.loadstore.ls_sid = sid;
1271
1272 return OK;
1273}
1274
1275/*
1276 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1277 */
1278 int
1279generate_VIM9SCRIPT(
1280 cctx_T *cctx,
1281 isntype_T isn_type,
1282 int sid,
1283 int idx,
1284 type_T *type)
1285{
1286 isn_T *isn;
1287 scriptref_T *sref;
1288 scriptitem_T *si = SCRIPT_ITEM(sid);
1289
1290 RETURN_OK_IF_SKIP(cctx);
1291 if (isn_type == ISN_LOADSCRIPT)
Bram Moolenaar160afdb2022-02-06 17:17:02 +00001292 isn = generate_instr_type2(cctx, isn_type, type, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001293 else
1294 isn = generate_instr_drop(cctx, isn_type, 1);
1295 if (isn == NULL)
1296 return FAIL;
1297
1298 // This requires three arguments, which doesn't fit in an instruction, thus
1299 // we need to allocate a struct for this.
1300 sref = ALLOC_ONE(scriptref_T);
1301 if (sref == NULL)
1302 return FAIL;
1303 isn->isn_arg.script.scriptref = sref;
1304 sref->sref_sid = sid;
1305 sref->sref_idx = idx;
1306 sref->sref_seq = si->sn_script_seq;
1307 sref->sref_type = type;
1308 return OK;
1309}
1310
1311/*
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001312 * Generate an ISN_NEWLIST instruction for "count" items.
1313 * "use_null" is TRUE for null_list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001314 */
1315 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001316generate_NEWLIST(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001317{
1318 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001319 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001320 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001321 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001322
1323 RETURN_OK_IF_SKIP(cctx);
1324 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1325 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001326 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001327
Bram Moolenaar078a4612022-01-04 15:17:03 +00001328 // Get the member type and the declared member type from all the items on
1329 // the stack.
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001330 member_type = get_member_type_from_stack(count, 1, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001331 type = get_list_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001332 decl_type = get_list_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001333
1334 // drop the value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001335 cctx->ctx_type_stack.ga_len -= count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001336
1337 // add the list type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001338 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001339}
1340
1341/*
1342 * Generate an ISN_NEWDICT instruction.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001343 * "use_null" is TRUE for null_dict.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001344 */
1345 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001346generate_NEWDICT(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001347{
1348 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001349 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001350 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001351 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001352
1353 RETURN_OK_IF_SKIP(cctx);
1354 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1355 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001356 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001357
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001358 member_type = get_member_type_from_stack(count, 2, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001359 type = get_dict_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001360 decl_type = get_dict_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001361
1362 // drop the key and value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001363 cctx->ctx_type_stack.ga_len -= 2 * count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001364
1365 // add the dict type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001366 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001367}
1368
1369/*
1370 * Generate an ISN_FUNCREF instruction.
Bram Moolenaar313e4722023-02-08 20:55:27 +00001371 * For "obj.Method" "cl" is the class of the object (can be an interface or a
1372 * base class) and "fi" the index of the method on that class.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001373 * "isnp" is set to the instruction, so that fr_dfunc_idx can be set later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001374 */
1375 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001376generate_FUNCREF(
1377 cctx_T *cctx,
1378 ufunc_T *ufunc,
Bram Moolenaar313e4722023-02-08 20:55:27 +00001379 class_T *cl,
1380 int fi,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001381 isn_T **isnp)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001382{
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001383 isn_T *isn;
1384 type_T *type;
1385 funcref_extra_T *extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001386 loopvarinfo_T loopinfo;
1387 int has_vars;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001388
1389 RETURN_OK_IF_SKIP(cctx);
1390 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1391 return FAIL;
Bram Moolenaara915fa02022-03-23 11:29:15 +00001392 if (isnp != NULL)
1393 *isnp = isn;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001394
Bram Moolenaarcc341812022-09-19 15:54:34 +01001395 has_vars = get_loop_var_info(cctx, &loopinfo);
Bram Moolenaar313e4722023-02-08 20:55:27 +00001396 if (ufunc->uf_def_status == UF_NOT_COMPILED || has_vars || cl != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001397 {
1398 extra = ALLOC_CLEAR_ONE(funcref_extra_T);
1399 if (extra == NULL)
1400 return FAIL;
1401 isn->isn_arg.funcref.fr_extra = extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001402 extra->fre_loopvar_info = loopinfo;
Bram Moolenaar313e4722023-02-08 20:55:27 +00001403 if (cl != NULL)
1404 {
1405 extra->fre_class = cl;
1406 ++cl->class_refcount;
1407 extra->fre_method_idx = fi;
1408 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001409 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00001410 if (ufunc->uf_def_status == UF_NOT_COMPILED || cl != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001411 extra->fre_func_name = vim_strsave(ufunc->uf_name);
Bram Moolenaar313e4722023-02-08 20:55:27 +00001412 if (ufunc->uf_def_status != UF_NOT_COMPILED && cl == NULL)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001413 {
1414 if (isnp == NULL && ufunc->uf_def_status == UF_TO_BE_COMPILED)
1415 // compile the function now, we need the uf_dfunc_idx value
1416 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001417 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001418 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01001419
1420 // Reserve an extra variable to keep track of the number of closures
1421 // created.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001422 cctx->ctx_has_closure = 1;
1423
1424 // If the referenced function is a closure, it may use items further up in
Bram Moolenaar139575d2022-03-15 19:29:30 +00001425 // the nested context, including this one. But not a function defined at
1426 // the script level.
1427 if ((ufunc->uf_flags & FC_CLOSURE)
1428 && func_name_refcount(cctx->ctx_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001429 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1430
Bram Moolenaar078a4612022-01-04 15:17:03 +00001431 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1432 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001433}
1434
1435/*
1436 * Generate an ISN_NEWFUNC instruction.
1437 * "lambda_name" and "func_name" must be in allocated memory and will be
1438 * consumed.
1439 */
1440 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001441generate_NEWFUNC(
1442 cctx_T *cctx,
1443 char_u *lambda_name,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001444 char_u *func_name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001445{
1446 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001447 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001448
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001449 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001450 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001451 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1452 ret = FAIL;
1453 else
1454 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001455 newfuncarg_T *arg = ALLOC_CLEAR_ONE(newfuncarg_T);
1456
1457 if (arg == NULL)
1458 ret = FAIL;
1459 else
1460 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01001461 // Reserve an extra variable to keep track of the number of
1462 // closures created.
1463 cctx->ctx_has_closure = 1;
1464
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001465 isn->isn_arg.newfunc.nf_arg = arg;
1466 arg->nfa_lambda = lambda_name;
1467 arg->nfa_global = func_name;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001468 (void)get_loop_var_info(cctx, &arg->nfa_loopvar_info);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001469 return OK;
1470 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001471 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001472 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001473 vim_free(lambda_name);
1474 vim_free(func_name);
1475 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001476}
1477
1478/*
1479 * Generate an ISN_DEF instruction: list functions
1480 */
1481 int
1482generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1483{
1484 isn_T *isn;
1485
1486 RETURN_OK_IF_SKIP(cctx);
1487 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1488 return FAIL;
1489 if (len > 0)
1490 {
1491 isn->isn_arg.string = vim_strnsave(name, len);
1492 if (isn->isn_arg.string == NULL)
1493 return FAIL;
1494 }
1495 return OK;
1496}
1497
1498/*
1499 * Generate an ISN_JUMP instruction.
1500 */
1501 int
1502generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1503{
1504 isn_T *isn;
1505 garray_T *stack = &cctx->ctx_type_stack;
1506
1507 RETURN_OK_IF_SKIP(cctx);
1508 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1509 return FAIL;
1510 isn->isn_arg.jump.jump_when = when;
1511 isn->isn_arg.jump.jump_where = where;
1512
1513 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1514 --stack->ga_len;
1515
1516 return OK;
1517}
1518
1519/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001520 * Generate an ISN_WHILE instruction. Similar to ISN_JUMP for :while
1521 */
1522 int
1523generate_WHILE(cctx_T *cctx, int funcref_idx)
1524{
1525 isn_T *isn;
1526 garray_T *stack = &cctx->ctx_type_stack;
1527
1528 RETURN_OK_IF_SKIP(cctx);
1529 if ((isn = generate_instr(cctx, ISN_WHILE)) == NULL)
1530 return FAIL;
1531 isn->isn_arg.whileloop.while_funcref_idx = funcref_idx;
1532 isn->isn_arg.whileloop.while_end = 0; // filled in later
1533
1534 if (stack->ga_len > 0)
1535 --stack->ga_len;
1536
1537 return OK;
1538}
1539
1540/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001541 * Generate an ISN_JUMP_IF_ARG_SET or ISN_JUMP_IF_ARG_NOT_SET instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001542 */
1543 int
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001544generate_JUMP_IF_ARG(cctx_T *cctx, isntype_T isn_type, int arg_off)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001545{
1546 isn_T *isn;
1547
1548 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001549 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001550 return FAIL;
1551 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1552 // jump_where is set later
1553 return OK;
1554}
1555
1556 int
1557generate_FOR(cctx_T *cctx, int loop_idx)
1558{
1559 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001560
1561 RETURN_OK_IF_SKIP(cctx);
1562 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1563 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001564 isn->isn_arg.forloop.for_loop_idx = loop_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001565
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001566 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001567 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001568}
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001569
1570 int
Bram Moolenaarcc341812022-09-19 15:54:34 +01001571generate_ENDLOOP(cctx_T *cctx, loop_info_T *loop_info)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001572{
1573 isn_T *isn;
1574
1575 RETURN_OK_IF_SKIP(cctx);
1576 if ((isn = generate_instr(cctx, ISN_ENDLOOP)) == NULL)
1577 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001578 isn->isn_arg.endloop.end_depth = loop_info->li_depth;
1579 isn->isn_arg.endloop.end_funcref_idx = loop_info->li_funcref_idx;
1580 isn->isn_arg.endloop.end_var_idx = loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001581 isn->isn_arg.endloop.end_var_count =
Bram Moolenaarcc341812022-09-19 15:54:34 +01001582 cctx->ctx_locals.ga_len - loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001583 return OK;
1584}
1585
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001586/*
1587 * Generate an ISN_TRYCONT instruction.
1588 */
1589 int
1590generate_TRYCONT(cctx_T *cctx, int levels, int where)
1591{
1592 isn_T *isn;
1593
1594 RETURN_OK_IF_SKIP(cctx);
1595 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1596 return FAIL;
1597 isn->isn_arg.trycont.tct_levels = levels;
1598 isn->isn_arg.trycont.tct_where = where;
1599
1600 return OK;
1601}
1602
Bram Moolenaar16900322022-09-08 19:51:45 +01001603/*
1604 * Check "argount" arguments and their types on the type stack.
1605 * Give an error and return FAIL if something is wrong.
1606 * When "method_call" is NULL no code is generated.
1607 */
1608 int
1609check_internal_func_args(
1610 cctx_T *cctx,
1611 int func_idx,
1612 int argcount,
1613 int method_call,
1614 type2_T **argtypes,
1615 type2_T *shuffled_argtypes)
1616{
1617 garray_T *stack = &cctx->ctx_type_stack;
1618 int argoff = check_internal_func(func_idx, argcount);
1619
1620 if (argoff < 0)
1621 return FAIL;
1622
1623 if (method_call && argoff > 1)
1624 {
1625 isn_T *isn = generate_instr(cctx, ISN_SHUFFLE);
1626
1627 if (isn == NULL)
1628 return FAIL;
1629 isn->isn_arg.shuffle.shfl_item = argcount;
1630 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1631 }
1632
1633 if (argcount > 0)
1634 {
1635 type2_T *typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
1636
1637 // Check the types of the arguments.
1638 if (method_call && argoff > 1)
1639 {
1640 int i;
1641
1642 for (i = 0; i < argcount; ++i)
1643 shuffled_argtypes[i] = (i < argoff - 1)
1644 ? typep[i + 1]
1645 : (i == argoff - 1) ? typep[0] : typep[i];
1646 *argtypes = shuffled_argtypes;
1647 }
1648 else
1649 {
1650 int i;
1651
1652 for (i = 0; i < argcount; ++i)
1653 shuffled_argtypes[i] = typep[i];
1654 *argtypes = shuffled_argtypes;
1655 }
1656 if (internal_func_check_arg_types(*argtypes, func_idx, argcount,
1657 cctx) == FAIL)
1658 return FAIL;
1659 }
1660 return OK;
1661}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001662
1663/*
1664 * Generate an ISN_BCALL instruction.
1665 * "method_call" is TRUE for "value->method()"
1666 * Return FAIL if the number of arguments is wrong.
1667 */
1668 int
1669generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1670{
1671 isn_T *isn;
1672 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001673 type2_T *argtypes = NULL;
1674 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1675 type2_T *maptype = NULL;
1676 type_T *type;
Bram Moolenaar81330182022-02-01 12:11:58 +00001677 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001678
1679 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar16900322022-09-08 19:51:45 +01001680
1681 if (check_internal_func_args(cctx, func_idx, argcount, method_call,
1682 &argtypes, shuffled_argtypes) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001683 return FAIL;
1684
Bram Moolenaar16900322022-09-08 19:51:45 +01001685 if (internal_func_is_map(func_idx))
1686 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001687
1688 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1689 return FAIL;
1690 isn->isn_arg.bfunc.cbf_idx = func_idx;
1691 isn->isn_arg.bfunc.cbf_argcount = argcount;
1692
1693 // Drop the argument types and push the return type.
1694 stack->ga_len -= argcount;
Bram Moolenaar32517c42023-01-15 18:17:12 +00001695 type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type,
1696 cctx->ctx_type_list);
Bram Moolenaar81330182022-02-01 12:11:58 +00001697 if (push_type_stack2(cctx, type, decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001698 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001699
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001700 if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
1701 && maptype[0].type_decl->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001702 // Check that map() didn't change the item types.
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001703 generate_TYPECHECK(cctx, maptype[0].type_decl, FALSE, -1, FALSE, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001704
1705 return OK;
1706}
1707
1708/*
1709 * Generate an ISN_LISTAPPEND instruction. Works like add().
1710 * Argument count is already checked.
1711 */
1712 int
1713generate_LISTAPPEND(cctx_T *cctx)
1714{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001715 type_T *list_type;
1716 type_T *item_type;
1717 type_T *expected;
1718
1719 // Caller already checked that list_type is a list.
Bram Moolenaar81330182022-02-01 12:11:58 +00001720 // For checking the item type we use the declared type of the list and the
1721 // current type of the added item, adding a string to [1, 2] is OK.
1722 list_type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaarfa103972022-09-29 19:14:42 +01001723 if (arg_type_modifiable(list_type, 1) == FAIL)
1724 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001725 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001726 expected = list_type->tt_member;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001727 if (need_type(item_type, expected, FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001728 return FAIL;
1729
1730 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1731 return FAIL;
1732
Bram Moolenaar078a4612022-01-04 15:17:03 +00001733 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001734 return OK;
1735}
1736
1737/*
1738 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1739 * Argument count is already checked.
1740 */
1741 int
1742generate_BLOBAPPEND(cctx_T *cctx)
1743{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001744 type_T *item_type;
1745
Bram Moolenaarfa103972022-09-29 19:14:42 +01001746 // Caller already checked that blob_type is a blob, check it is modifiable.
1747 if (arg_type_modifiable(get_decl_type_on_stack(cctx, 1), 1) == FAIL)
1748 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001749 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001750 if (need_type(item_type, &t_number, FALSE,
1751 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001752 return FAIL;
1753
1754 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1755 return FAIL;
1756
Bram Moolenaar078a4612022-01-04 15:17:03 +00001757 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001758 return OK;
1759}
1760
1761/*
Bram Moolenaard0200c82023-01-28 15:19:40 +00001762 * Generate an ISN_DCALL, ISN_UCALL or ISN_METHODCALL instruction.
1763 * When calling a method on an object, of which we know the interface only,
1764 * then "cl" is the interface and "mi" the method index on the interface.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001765 * Return FAIL if the number of arguments is wrong.
1766 */
1767 int
Bram Moolenaard0200c82023-01-28 15:19:40 +00001768generate_CALL(
1769 cctx_T *cctx,
1770 ufunc_T *ufunc,
1771 class_T *cl,
1772 int mi,
1773 int pushed_argcount)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001774{
1775 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001776 int regular_args = ufunc->uf_args.ga_len;
1777 int argcount = pushed_argcount;
1778
1779 RETURN_OK_IF_SKIP(cctx);
1780 if (argcount > regular_args && !has_varargs(ufunc))
1781 {
1782 semsg(_(e_too_many_arguments_for_function_str),
1783 printable_func_name(ufunc));
1784 return FAIL;
1785 }
1786 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1787 {
1788 semsg(_(e_not_enough_arguments_for_function_str),
1789 printable_func_name(ufunc));
1790 return FAIL;
1791 }
1792
1793 if (ufunc->uf_def_status != UF_NOT_COMPILED
1794 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1795 {
1796 int i;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001797 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001798
1799 for (i = 0; i < argcount; ++i)
1800 {
1801 type_T *expected;
1802 type_T *actual;
1803
Bram Moolenaar078a4612022-01-04 15:17:03 +00001804 actual = get_type_on_stack(cctx, argcount - i - 1);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001805 if (actual->tt_type == VAR_SPECIAL
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001806 && i >= regular_args - ufunc->uf_def_args.ga_len)
1807 {
1808 // assume v:none used for default argument value
1809 continue;
1810 }
1811 if (i < regular_args)
1812 {
1813 if (ufunc->uf_arg_types == NULL)
1814 continue;
1815 expected = ufunc->uf_arg_types[i];
1816 }
1817 else if (ufunc->uf_va_type == NULL
1818 || ufunc->uf_va_type == &t_list_any)
1819 // possibly a lambda or "...: any"
1820 expected = &t_any;
1821 else
1822 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001823 if (need_type(actual, expected, FALSE,
1824 -argcount + i, i + 1, cctx, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001825 {
1826 arg_type_mismatch(expected, actual, i + 1);
1827 return FAIL;
1828 }
1829 }
Bram Moolenaar139575d2022-03-15 19:29:30 +00001830 compile_type = get_compile_type(ufunc);
1831 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001832 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaar139575d2022-03-15 19:29:30 +00001833 compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001834 return FAIL;
1835 }
1836 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1837 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001838 emsg_funcname(e_call_to_function_that_failed_to_compile_str,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001839 ufunc->uf_name);
1840 return FAIL;
1841 }
1842
Bram Moolenaard0200c82023-01-28 15:19:40 +00001843 if ((isn = generate_instr(cctx, cl != NULL ? ISN_METHODCALL
1844 : ufunc->uf_def_status != UF_NOT_COMPILED
1845 ? ISN_DCALL : ISN_UCALL)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001846 return FAIL;
Bram Moolenaar836137d2023-01-29 14:11:24 +00001847 if (cl != NULL /* isn->isn_type == ISN_METHODCALL */)
Bram Moolenaard0200c82023-01-28 15:19:40 +00001848 {
1849 isn->isn_arg.mfunc = ALLOC_ONE(cmfunc_T);
1850 if (isn->isn_arg.mfunc == NULL)
1851 return FAIL;
1852 isn->isn_arg.mfunc->cmf_itf = cl;
1853 ++cl->class_refcount;
1854 isn->isn_arg.mfunc->cmf_idx = mi;
1855 isn->isn_arg.mfunc->cmf_argcount = argcount;
1856 }
1857 else if (isn->isn_type == ISN_DCALL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001858 {
1859 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1860 isn->isn_arg.dfunc.cdf_argcount = argcount;
1861 }
1862 else
1863 {
1864 // A user function may be deleted and redefined later, can't use the
1865 // ufunc pointer, need to look it up again at runtime.
1866 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1867 isn->isn_arg.ufunc.cuf_argcount = argcount;
1868 }
1869
Bram Moolenaar078a4612022-01-04 15:17:03 +00001870 // drop the argument types
1871 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001872
Bram Moolenaar078a4612022-01-04 15:17:03 +00001873 // add return type
1874 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001875}
1876
1877/*
1878 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1879 */
1880 int
1881generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1882{
1883 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001884
1885 RETURN_OK_IF_SKIP(cctx);
1886 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1887 return FAIL;
1888 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1889 isn->isn_arg.ufunc.cuf_argcount = argcount;
1890
Bram Moolenaar078a4612022-01-04 15:17:03 +00001891 // drop the argument types
1892 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001893
Bram Moolenaar078a4612022-01-04 15:17:03 +00001894 // add return value
1895 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001896}
1897
1898/*
Bram Moolenaar16900322022-09-08 19:51:45 +01001899 * Check the arguments of function "type" against the types on the stack.
1900 * Returns OK or FAIL;
1901 */
1902 int
1903check_func_args_from_type(
1904 cctx_T *cctx,
1905 type_T *type,
1906 int argcount,
1907 int at_top,
1908 char_u *name)
1909{
1910 if (type->tt_argcount != -1)
1911 {
1912 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1913
1914 if (argcount < type->tt_min_argcount - varargs)
1915 {
1916 emsg_funcname(e_not_enough_arguments_for_function_str, name);
1917 return FAIL;
1918 }
1919 if (!varargs && argcount > type->tt_argcount)
1920 {
1921 emsg_funcname(e_too_many_arguments_for_function_str, name);
1922 return FAIL;
1923 }
1924 if (type->tt_args != NULL)
1925 {
1926 int i;
1927
1928 for (i = 0; i < argcount; ++i)
1929 {
1930 int offset = -argcount + i - (at_top ? 0 : 1);
1931 type_T *actual = get_type_on_stack(cctx, -1 - offset);
1932 type_T *expected;
1933
1934 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaar36818a92023-01-03 12:33:26 +00001935 {
1936 expected = type->tt_args[type->tt_argcount - 1];
1937 if (expected != NULL && expected->tt_type == VAR_LIST)
1938 expected = expected->tt_member;
1939 if (expected == NULL)
1940 expected = &t_any;
1941 }
Bram Moolenaar16900322022-09-08 19:51:45 +01001942 else if (i >= type->tt_min_argcount
1943 && actual->tt_type == VAR_SPECIAL)
1944 expected = &t_any;
1945 else
1946 expected = type->tt_args[i];
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001947 if (need_type(actual, expected, FALSE,
1948 offset, i + 1, cctx, TRUE, FALSE) == FAIL)
Bram Moolenaar16900322022-09-08 19:51:45 +01001949 {
1950 arg_type_mismatch(expected, actual, i + 1);
1951 return FAIL;
1952 }
1953 }
1954 }
1955 }
1956
1957 return OK;
1958}
1959/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001960 * Generate an ISN_PCALL instruction.
1961 * "type" is the type of the FuncRef.
1962 */
1963 int
1964generate_PCALL(
1965 cctx_T *cctx,
1966 int argcount,
1967 char_u *name,
1968 type_T *type,
1969 int at_top)
1970{
1971 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001972 type_T *ret_type;
1973
1974 RETURN_OK_IF_SKIP(cctx);
1975
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001976 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001977 ret_type = &t_any;
1978 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1979 {
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00001980 if (check_func_args_from_type(cctx, type, argcount, at_top, name)
1981 == FAIL)
Bram Moolenaar16900322022-09-08 19:51:45 +01001982 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001983
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001984 ret_type = type->tt_member;
1985 if (ret_type == &t_unknown)
1986 // return type not known yet, use a runtime check
1987 ret_type = &t_any;
1988 }
1989 else
1990 {
1991 semsg(_(e_not_callable_type_str), name);
1992 return FAIL;
1993 }
1994
1995 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1996 return FAIL;
1997 isn->isn_arg.pfunc.cpf_top = at_top;
1998 isn->isn_arg.pfunc.cpf_argcount = argcount;
1999
Bram Moolenaar078a4612022-01-04 15:17:03 +00002000 // drop the arguments and the funcref/partial
2001 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002002
Bram Moolenaar078a4612022-01-04 15:17:03 +00002003 // push the return value
2004 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002005
2006 // If partial is above the arguments it must be cleared and replaced with
2007 // the return value.
2008 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2009 return FAIL;
2010
2011 return OK;
2012}
2013
2014/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002015 * Generate an ISN_DEFER instruction.
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00002016 * "obj_method" is one for "obj.Method()", zero otherwise.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002017 */
2018 int
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00002019generate_DEFER(cctx_T *cctx, int var_idx, int obj_method, int argcount)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002020{
2021 isn_T *isn;
2022
2023 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00002024 if ((isn = generate_instr_drop(cctx,
2025 obj_method == 0 ? ISN_DEFER : ISN_DEFEROBJ,
2026 argcount + 1)) == NULL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002027 return FAIL;
2028 isn->isn_arg.defer.defer_var_idx = var_idx;
2029 isn->isn_arg.defer.defer_argcount = argcount;
2030 return OK;
2031}
2032
2033/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002034 * Generate an ISN_STRINGMEMBER instruction.
2035 */
2036 int
2037generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
2038{
2039 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002040 type_T *type;
2041
2042 RETURN_OK_IF_SKIP(cctx);
2043 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
2044 return FAIL;
2045 isn->isn_arg.string = vim_strnsave(name, len);
2046
2047 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00002048 type = get_type_on_stack(cctx, 0);
Bram Moolenaar0089ce22022-10-08 14:39:36 +01002049 if (type->tt_type != VAR_DICT
2050 && type->tt_type != VAR_ANY && type->tt_type != VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002051 {
2052 char *tofree;
2053
2054 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
2055 name, type_name(type, &tofree));
2056 vim_free(tofree);
2057 return FAIL;
2058 }
2059 // change dict type to dict member type
2060 if (type->tt_type == VAR_DICT)
2061 {
Bram Moolenaar0089ce22022-10-08 14:39:36 +01002062 type_T *ntype = type->tt_member->tt_type == VAR_UNKNOWN
Bram Moolenaar078a4612022-01-04 15:17:03 +00002063 ? &t_any : type->tt_member;
2064 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002065 }
2066
2067 return OK;
2068}
2069
2070/*
2071 * Generate an ISN_ECHO instruction.
2072 */
2073 int
2074generate_ECHO(cctx_T *cctx, int with_white, int count)
2075{
2076 isn_T *isn;
2077
2078 RETURN_OK_IF_SKIP(cctx);
2079 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2080 return FAIL;
2081 isn->isn_arg.echo.echo_with_white = with_white;
2082 isn->isn_arg.echo.echo_count = count;
2083
2084 return OK;
2085}
2086
2087/*
2088 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
2089 */
2090 int
2091generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
2092{
2093 isn_T *isn;
2094
Bram Moolenaar2eae3d22022-10-07 15:09:27 +01002095 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002096 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
2097 return FAIL;
2098 isn->isn_arg.number = count;
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002099 return OK;
2100}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002101
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002102/*
2103 * Generate an ISN_ECHOWINDOW instruction
2104 */
2105 int
2106generate_ECHOWINDOW(cctx_T *cctx, int count, long time)
2107{
2108 isn_T *isn;
2109
2110 if ((isn = generate_instr_drop(cctx, ISN_ECHOWINDOW, count)) == NULL)
2111 return FAIL;
2112 isn->isn_arg.echowin.ewin_count = count;
2113 isn->isn_arg.echowin.ewin_time = time;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002114 return OK;
2115}
2116
2117/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002118 * Generate an ISN_SOURCE instruction.
2119 */
2120 int
2121generate_SOURCE(cctx_T *cctx, int sid)
2122{
2123 isn_T *isn;
2124
2125 if ((isn = generate_instr(cctx, ISN_SOURCE)) == NULL)
2126 return FAIL;
2127 isn->isn_arg.number = sid;
2128
2129 return OK;
2130}
2131
2132/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002133 * Generate an ISN_PUT instruction.
2134 */
2135 int
2136generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2137{
2138 isn_T *isn;
2139
2140 RETURN_OK_IF_SKIP(cctx);
2141 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2142 return FAIL;
2143 isn->isn_arg.put.put_regname = regname;
2144 isn->isn_arg.put.put_lnum = lnum;
2145 return OK;
2146}
2147
2148/*
2149 * Generate an EXEC instruction that takes a string argument.
2150 * A copy is made of "line".
2151 */
2152 int
2153generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
2154{
2155 isn_T *isn;
2156
2157 RETURN_OK_IF_SKIP(cctx);
2158 if ((isn = generate_instr(cctx, isntype)) == NULL)
2159 return FAIL;
2160 isn->isn_arg.string = vim_strsave(line);
2161 return OK;
2162}
2163
2164/*
2165 * Generate an EXEC instruction that takes a string argument.
2166 * "str" must be allocated, it is consumed.
2167 */
2168 int
2169generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
2170{
2171 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002172 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002173
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002174 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002175 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002176 if ((isn = generate_instr(cctx, isntype)) == NULL)
2177 ret = FAIL;
2178 else
2179 {
2180 isn->isn_arg.string = str;
2181 return OK;
2182 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002183 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002184 vim_free(str);
2185 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002186}
2187
2188 int
2189generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2190{
2191 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002192
2193 RETURN_OK_IF_SKIP(cctx);
2194 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2195 return FAIL;
2196 isn->isn_arg.string = vim_strsave(line);
2197
Bram Moolenaar078a4612022-01-04 15:17:03 +00002198 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002199}
2200
2201 int
2202generate_EXECCONCAT(cctx_T *cctx, int count)
2203{
2204 isn_T *isn;
2205
2206 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2207 return FAIL;
2208 isn->isn_arg.number = count;
2209 return OK;
2210}
2211
2212/*
2213 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2214 */
2215 int
2216generate_RANGE(cctx_T *cctx, char_u *range)
2217{
2218 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002219
2220 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2221 return FAIL;
2222 isn->isn_arg.string = range;
2223
Bram Moolenaar078a4612022-01-04 15:17:03 +00002224 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002225}
2226
2227 int
2228generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2229{
2230 isn_T *isn;
2231
2232 RETURN_OK_IF_SKIP(cctx);
2233 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2234 return FAIL;
2235 isn->isn_arg.unpack.unp_count = var_count;
2236 isn->isn_arg.unpack.unp_semicolon = semicolon;
2237 return OK;
2238}
2239
2240/*
2241 * Generate an instruction for any command modifiers.
2242 */
2243 int
2244generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
2245{
2246 isn_T *isn;
2247
2248 if (has_cmdmod(cmod, FALSE))
2249 {
2250 cctx->ctx_has_cmdmod = TRUE;
2251
2252 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
2253 return FAIL;
2254 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2255 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2256 return FAIL;
2257 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
2258 // filter program now belongs to the instruction
2259 cmod->cmod_filter_regmatch.regprog = NULL;
2260 }
2261
2262 return OK;
2263}
2264
2265 int
2266generate_undo_cmdmods(cctx_T *cctx)
2267{
2268 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2269 return FAIL;
2270 cctx->ctx_has_cmdmod = FALSE;
2271 return OK;
2272}
2273
2274/*
2275 * Generate a STORE instruction for "dest", not being "dest_local".
Bram Moolenaard505d172022-12-18 21:42:55 +00002276 * "lhs" might be NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002277 * Return FAIL when out of memory.
2278 */
2279 int
2280generate_store_var(
2281 cctx_T *cctx,
2282 assign_dest_T dest,
2283 int opt_flags,
2284 int vimvaridx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002285 type_T *type,
Bram Moolenaard505d172022-12-18 21:42:55 +00002286 char_u *name,
2287 lhs_T *lhs)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002288{
2289 switch (dest)
2290 {
2291 case dest_option:
2292 return generate_STOREOPT(cctx, ISN_STOREOPT,
2293 skip_option_env_lead(name), opt_flags);
2294 case dest_func_option:
2295 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
2296 skip_option_env_lead(name), opt_flags);
2297 case dest_global:
2298 // include g: with the name, easier to execute that way
2299 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
2300 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
2301 case dest_buffer:
2302 // include b: with the name, easier to execute that way
2303 return generate_STORE(cctx, ISN_STOREB, 0, name);
2304 case dest_window:
2305 // include w: with the name, easier to execute that way
2306 return generate_STORE(cctx, ISN_STOREW, 0, name);
2307 case dest_tab:
2308 // include t: with the name, easier to execute that way
2309 return generate_STORE(cctx, ISN_STORET, 0, name);
2310 case dest_env:
2311 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
2312 case dest_reg:
2313 return generate_STORE(cctx, ISN_STOREREG,
2314 name[1] == '@' ? '"' : name[1], NULL);
2315 case dest_vimvar:
2316 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
2317 case dest_script:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002318 {
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002319 int scriptvar_idx = lhs->lhs_scriptvar_idx;
2320 int scriptvar_sid = lhs->lhs_scriptvar_sid;
2321 if (scriptvar_idx < 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002322 {
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002323 isntype_T isn_type = ISN_STORES;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002324
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002325 if (SCRIPT_ID_VALID(scriptvar_sid)
2326 && SCRIPT_ITEM(scriptvar_sid)->sn_import_autoload
2327 && SCRIPT_ITEM(scriptvar_sid)->sn_autoload_prefix
2328 == NULL)
2329 {
2330 // "import autoload './dir/script.vim'" - load script
2331 // first
2332 if (generate_SOURCE(cctx, scriptvar_sid) == FAIL)
2333 return FAIL;
2334 isn_type = ISN_STOREEXPORT;
2335 }
2336
2337 // "s:" may be included in the name.
2338 return generate_OLDSCRIPT(cctx, isn_type, name,
2339 scriptvar_sid, type);
2340 }
2341 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002342 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002343 }
Bram Moolenaard505d172022-12-18 21:42:55 +00002344 case dest_class_member:
2345 return generate_CLASSMEMBER(cctx, FALSE,
2346 lhs->lhs_class, lhs->lhs_classmember_idx);
2347
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002348 case dest_local:
2349 case dest_expr:
2350 // cannot happen
2351 break;
2352 }
2353 return FAIL;
2354}
2355
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002356/*
2357 * Return TRUE when inside a "for" or "while" loop.
2358 */
2359 int
2360inside_loop_scope(cctx_T *cctx)
2361{
2362 scope_T *scope = cctx->ctx_scope;
2363
2364 for (;;)
2365 {
2366 if (scope == NULL)
2367 break;
2368 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
2369 return TRUE;
2370 scope = scope->se_outer;
2371 }
2372 return FALSE;
2373}
2374
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002375 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002376generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002377{
2378 if (lhs->lhs_dest != dest_local)
2379 return generate_store_var(cctx, lhs->lhs_dest,
2380 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
Bram Moolenaard505d172022-12-18 21:42:55 +00002381 lhs->lhs_type, lhs->lhs_name, lhs);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002382
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002383 if (lhs->lhs_lvar == NULL)
2384 return OK;
2385
2386 garray_T *instr = &cctx->ctx_instr;
2387 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2388
2389 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
2390 // ISN_STORENR.
2391 // And "var = 0" does not need any instruction.
2392 if (lhs->lhs_lvar->lv_from_outer == 0
2393 && instr->ga_len == instr_count + 1
2394 && isn->isn_type == ISN_PUSHNR)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002395 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002396 varnumber_T val = isn->isn_arg.number;
2397 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002398
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002399 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002400 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002401 // zero is the default value, no need to do anything
2402 --instr->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002403 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002404 else
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002405 {
2406 isn->isn_type = ISN_STORENR;
2407 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
2408 isn->isn_arg.storenr.stnr_val = val;
2409 }
2410 if (stack->ga_len > 0)
2411 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002412 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002413 else if (lhs->lhs_lvar->lv_from_outer > 0)
2414 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
2415 lhs->lhs_lvar->lv_from_outer, lhs->lhs_lvar->lv_loop_idx);
2416 else
2417 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002418 return OK;
2419}
2420
2421#if defined(FEAT_PROFILE) || defined(PROTO)
2422 void
2423may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2424{
2425 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
2426 generate_instr(cctx, ISN_PROF_END);
2427}
2428#endif
2429
2430
2431/*
2432 * Delete an instruction, free what it contains.
2433 */
2434 void
2435delete_instr(isn_T *isn)
2436{
2437 switch (isn->isn_type)
2438 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002439 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002440 case ISN_DEF:
2441 case ISN_EXEC:
2442 case ISN_EXECRANGE:
2443 case ISN_EXEC_SPLIT:
2444 case ISN_LEGACY_EVAL:
2445 case ISN_LOADAUTO:
2446 case ISN_LOADB:
2447 case ISN_LOADENV:
2448 case ISN_LOADG:
2449 case ISN_LOADOPT:
2450 case ISN_LOADT:
2451 case ISN_LOADW:
2452 case ISN_LOCKUNLOCK:
2453 case ISN_PUSHEXC:
2454 case ISN_PUSHFUNC:
2455 case ISN_PUSHS:
2456 case ISN_RANGE:
2457 case ISN_STOREAUTO:
2458 case ISN_STOREB:
2459 case ISN_STOREENV:
2460 case ISN_STOREG:
2461 case ISN_STORET:
2462 case ISN_STOREW:
2463 case ISN_STRINGMEMBER:
2464 vim_free(isn->isn_arg.string);
2465 break;
2466
2467 case ISN_SUBSTITUTE:
2468 {
2469 int idx;
2470 isn_T *list = isn->isn_arg.subs.subs_instr;
2471
2472 vim_free(isn->isn_arg.subs.subs_cmd);
2473 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2474 delete_instr(list + idx);
2475 vim_free(list);
2476 }
2477 break;
2478
2479 case ISN_INSTR:
2480 {
2481 int idx;
2482 isn_T *list = isn->isn_arg.instr;
2483
2484 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2485 delete_instr(list + idx);
2486 vim_free(list);
2487 }
2488 break;
2489
2490 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002491 case ISN_LOADEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002492 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002493 case ISN_STOREEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002494 vim_free(isn->isn_arg.loadstore.ls_name);
2495 break;
2496
2497 case ISN_UNLET:
2498 case ISN_UNLETENV:
2499 vim_free(isn->isn_arg.unlet.ul_name);
2500 break;
2501
2502 case ISN_STOREOPT:
2503 case ISN_STOREFUNCOPT:
2504 vim_free(isn->isn_arg.storeopt.so_name);
2505 break;
2506
2507 case ISN_PUSHBLOB: // push blob isn_arg.blob
2508 blob_unref(isn->isn_arg.blob);
2509 break;
2510
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002511 case ISN_PUSHCLASS:
2512 class_unref(isn->isn_arg.class);
2513 break;
2514
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002515 case ISN_UCALL:
2516 vim_free(isn->isn_arg.ufunc.cuf_name);
2517 break;
2518
2519 case ISN_FUNCREF:
2520 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002521 funcref_T *funcref = &isn->isn_arg.funcref;
2522 funcref_extra_T *extra = funcref->fr_extra;
2523
2524 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002525 {
2526 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002527 + funcref->fr_dfunc_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002528 ufunc_T *ufunc = dfunc->df_ufunc;
2529
2530 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2531 func_ptr_unref(ufunc);
2532 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002533 if (extra != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002534 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002535 char_u *name = extra->fre_func_name;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002536
2537 if (name != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002538 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002539 func_unref(name);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002540 vim_free(name);
2541 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00002542 if (extra->fre_class != NULL)
2543 class_unref(extra->fre_class);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002544 vim_free(extra);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002545 }
2546 }
2547 break;
2548
2549 case ISN_DCALL:
2550 {
2551 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002552 + isn->isn_arg.dfunc.cdf_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002553
2554 if (dfunc->df_ufunc != NULL
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002555 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002556 func_ptr_unref(dfunc->df_ufunc);
2557 }
2558 break;
2559
Bram Moolenaard0200c82023-01-28 15:19:40 +00002560 case ISN_METHODCALL:
2561 {
2562 cmfunc_T *mfunc = isn->isn_arg.mfunc;
2563 class_unref(mfunc->cmf_itf);
2564 vim_free(mfunc);
2565 }
2566 break;
2567
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002568 case ISN_NEWFUNC:
2569 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002570 newfuncarg_T *arg = isn->isn_arg.newfunc.nf_arg;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002571
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002572 if (arg != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002573 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002574 ufunc_T *ufunc = find_func_even_dead(
2575 arg->nfa_lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002576
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002577 if (ufunc != NULL)
2578 {
2579 unlink_def_function(ufunc);
2580 func_ptr_unref(ufunc);
2581 }
2582
2583 vim_free(arg->nfa_lambda);
2584 vim_free(arg->nfa_global);
2585 vim_free(arg);
2586 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002587 }
2588 break;
2589
2590 case ISN_CHECKTYPE:
2591 case ISN_SETTYPE:
2592 free_type(isn->isn_arg.type.ct_type);
2593 break;
2594
2595 case ISN_CMDMOD:
2596 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002597 ->cmod_filter_regmatch.regprog);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002598 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2599 break;
2600
2601 case ISN_LOADSCRIPT:
2602 case ISN_STORESCRIPT:
2603 vim_free(isn->isn_arg.script.scriptref);
2604 break;
2605
Bram Moolenaard505d172022-12-18 21:42:55 +00002606 case ISN_LOAD_CLASSMEMBER:
2607 case ISN_STORE_CLASSMEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00002608 case ISN_GET_ITF_MEMBER:
Bram Moolenaard505d172022-12-18 21:42:55 +00002609 class_unref(isn->isn_arg.classmember.cm_class);
2610 break;
2611
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002612 case ISN_STOREINDEX:
2613 class_unref(isn->isn_arg.storeindex.si_class);
2614 break;
2615
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002616 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002617 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002618 break;
2619
2620 case ISN_CEXPR_CORE:
2621 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2622 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2623 break;
2624
2625 case ISN_2BOOL:
2626 case ISN_2STRING:
2627 case ISN_2STRING_ANY:
2628 case ISN_ADDBLOB:
2629 case ISN_ADDLIST:
2630 case ISN_ANYINDEX:
2631 case ISN_ANYSLICE:
2632 case ISN_BCALL:
2633 case ISN_BLOBAPPEND:
2634 case ISN_BLOBINDEX:
2635 case ISN_BLOBSLICE:
2636 case ISN_CATCH:
2637 case ISN_CEXPR_AUCMD:
2638 case ISN_CHECKLEN:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002639 case ISN_CLEARDICT:
2640 case ISN_CMDMOD_REV:
2641 case ISN_COMPAREANY:
2642 case ISN_COMPAREBLOB:
2643 case ISN_COMPAREBOOL:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002644 case ISN_COMPARECLASS:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002645 case ISN_COMPAREDICT:
2646 case ISN_COMPAREFLOAT:
2647 case ISN_COMPAREFUNC:
2648 case ISN_COMPARELIST:
2649 case ISN_COMPARENR:
Bram Moolenaar7a222242022-03-01 19:23:24 +00002650 case ISN_COMPARENULL:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002651 case ISN_COMPAREOBJECT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002652 case ISN_COMPARESPECIAL:
2653 case ISN_COMPARESTRING:
2654 case ISN_CONCAT:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002655 case ISN_CONSTRUCT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002656 case ISN_COND2BOOL:
2657 case ISN_DEBUG:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002658 case ISN_DEFER:
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00002659 case ISN_DEFEROBJ:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002660 case ISN_DROP:
2661 case ISN_ECHO:
2662 case ISN_ECHOCONSOLE:
2663 case ISN_ECHOERR:
2664 case ISN_ECHOMSG:
Bram Moolenaar68a635a2022-09-01 17:26:17 +01002665 case ISN_ECHOWINDOW:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002666 case ISN_ENDLOOP:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002667 case ISN_ENDTRY:
2668 case ISN_EXECCONCAT:
2669 case ISN_EXECUTE:
2670 case ISN_FINALLY:
2671 case ISN_FINISH:
2672 case ISN_FOR:
2673 case ISN_GETITEM:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002674 case ISN_GET_OBJ_MEMBER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002675 case ISN_JUMP:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002676 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002677 case ISN_JUMP_IF_ARG_SET:
2678 case ISN_LISTAPPEND:
2679 case ISN_LISTINDEX:
2680 case ISN_LISTSLICE:
2681 case ISN_LOAD:
2682 case ISN_LOADBDICT:
2683 case ISN_LOADGDICT:
2684 case ISN_LOADOUTER:
2685 case ISN_LOADREG:
2686 case ISN_LOADTDICT:
2687 case ISN_LOADV:
2688 case ISN_LOADWDICT:
2689 case ISN_LOCKCONST:
2690 case ISN_MEMBER:
2691 case ISN_NEGATENR:
2692 case ISN_NEWDICT:
2693 case ISN_NEWLIST:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002694 case ISN_NEWPARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002695 case ISN_OPANY:
2696 case ISN_OPFLOAT:
2697 case ISN_OPNR:
2698 case ISN_PCALL:
2699 case ISN_PCALL_END:
2700 case ISN_PROF_END:
2701 case ISN_PROF_START:
2702 case ISN_PUSHBOOL:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002703 case ISN_PUSHCHANNEL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002704 case ISN_PUSHF:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002705 case ISN_PUSHJOB:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002706 case ISN_PUSHNR:
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002707 case ISN_PUSHOBJ:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002708 case ISN_PUSHSPEC:
2709 case ISN_PUT:
2710 case ISN_REDIREND:
2711 case ISN_REDIRSTART:
2712 case ISN_RETURN:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002713 case ISN_RETURN_OBJECT:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002714 case ISN_RETURN_VOID:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002715 case ISN_SHUFFLE:
2716 case ISN_SLICE:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002717 case ISN_SOURCE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002718 case ISN_STORE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002719 case ISN_STORENR:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002720 case ISN_STOREOUTER:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002721 case ISN_STORE_THIS:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002722 case ISN_STORERANGE:
2723 case ISN_STOREREG:
2724 case ISN_STOREV:
2725 case ISN_STRINDEX:
2726 case ISN_STRSLICE:
2727 case ISN_THROW:
2728 case ISN_TRYCONT:
2729 case ISN_UNLETINDEX:
2730 case ISN_UNLETRANGE:
2731 case ISN_UNPACK:
2732 case ISN_USEDICT:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002733 case ISN_WHILE:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002734 // nothing allocated
2735 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002736 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002737}
2738
2739 void
2740clear_instr_ga(garray_T *gap)
2741{
2742 int idx;
2743
2744 for (idx = 0; idx < gap->ga_len; ++idx)
2745 delete_instr(((isn_T *)gap->ga_data) + idx);
2746 ga_clear(gap);
2747}
2748
2749
2750#endif // defined(FEAT_EVAL)