blob: cc0d8ad8b44f29f8ce3bd3212522b0ed289b939a [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/*
659 * Generate a PUSH instruction for "tv".
660 * "tv" will be consumed or cleared.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000661 */
662 int
663generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
664{
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100665 switch (tv->v_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000666 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100667 case VAR_BOOL:
668 generate_PUSHBOOL(cctx, tv->vval.v_number);
669 break;
670 case VAR_SPECIAL:
671 generate_PUSHSPEC(cctx, tv->vval.v_number);
672 break;
673 case VAR_NUMBER:
674 generate_PUSHNR(cctx, tv->vval.v_number);
675 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100676 case VAR_FLOAT:
677 generate_PUSHF(cctx, tv->vval.v_float);
678 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100679 case VAR_BLOB:
680 generate_PUSHBLOB(cctx, tv->vval.v_blob);
681 tv->vval.v_blob = NULL;
682 break;
683 case VAR_LIST:
684 if (tv->vval.v_list != NULL)
685 iemsg("non-empty list constant not supported");
686 generate_NEWLIST(cctx, 0, TRUE);
687 break;
688 case VAR_DICT:
689 if (tv->vval.v_dict != NULL)
690 iemsg("non-empty dict constant not supported");
691 generate_NEWDICT(cctx, 0, TRUE);
692 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000693#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100694 case VAR_JOB:
695 if (tv->vval.v_job != NULL)
696 iemsg("non-null job constant not supported");
697 generate_PUSHJOB(cctx);
698 break;
699 case VAR_CHANNEL:
700 if (tv->vval.v_channel != NULL)
701 iemsg("non-null channel constant not supported");
702 generate_PUSHCHANNEL(cctx);
703 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000704#endif
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100705 case VAR_FUNC:
706 if (tv->vval.v_string != NULL)
707 iemsg("non-null function constant not supported");
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100708 generate_PUSHFUNC(cctx, NULL, &t_func_unknown, TRUE);
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100709 break;
710 case VAR_PARTIAL:
711 if (tv->vval.v_partial != NULL)
712 iemsg("non-null partial constant not supported");
713 if (generate_instr_type(cctx, ISN_NEWPARTIAL, &t_func_unknown)
714 == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000715 return FAIL;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100716 break;
717 case VAR_STRING:
718 generate_PUSHS(cctx, &tv->vval.v_string);
719 tv->vval.v_string = NULL;
720 break;
721 default:
722 siemsg("constant type %d not supported", tv->v_type);
723 clear_tv(tv);
724 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000725 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100726 tv->v_type = VAR_UNKNOWN;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000727 return OK;
728}
729
730/*
731 * Generate an ISN_PUSHNR instruction.
732 */
733 int
734generate_PUSHNR(cctx_T *cctx, varnumber_T number)
735{
736 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000737
738 RETURN_OK_IF_SKIP(cctx);
739 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
740 return FAIL;
741 isn->isn_arg.number = number;
742
743 if (number == 0 || number == 1)
744 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000745 set_type_on_stack(cctx, &t_number_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000746 return OK;
747}
748
749/*
750 * Generate an ISN_PUSHBOOL instruction.
751 */
752 int
753generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
754{
755 isn_T *isn;
756
757 RETURN_OK_IF_SKIP(cctx);
758 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
759 return FAIL;
760 isn->isn_arg.number = number;
761
762 return OK;
763}
764
765/*
766 * Generate an ISN_PUSHSPEC instruction.
767 */
768 int
769generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
770{
771 isn_T *isn;
772
773 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000774 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC,
775 number == VVAL_NULL ? &t_null : &t_none)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000776 return FAIL;
777 isn->isn_arg.number = number;
778
779 return OK;
780}
781
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000782/*
783 * Generate an ISN_PUSHF instruction.
784 */
785 int
786generate_PUSHF(cctx_T *cctx, float_T fnumber)
787{
788 isn_T *isn;
789
790 RETURN_OK_IF_SKIP(cctx);
791 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
792 return FAIL;
793 isn->isn_arg.fnumber = fnumber;
794
795 return OK;
796}
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000797
798/*
799 * Generate an ISN_PUSHS instruction.
800 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100801 * Note that if "str" is used in the instruction OK is returned and "*str" is
802 * not set to NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000803 */
804 int
805generate_PUSHS(cctx_T *cctx, char_u **str)
806{
807 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100808 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000809
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100810 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000811 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100812 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
813 ret = FAIL;
814 else
815 {
816 isn->isn_arg.string = str == NULL ? NULL : *str;
817 return OK;
818 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000819 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100820 if (str != NULL)
821 VIM_CLEAR(*str);
822 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000823}
824
825/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000826 * Generate an ISN_PUSHCHANNEL instruction. Channel is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000827 */
828 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000829generate_PUSHCHANNEL(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000830{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000831 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100832#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100833 if (generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000834 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000835 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100836#else
837 emsg(_(e_channel_job_feature_not_available));
838 return FAIL;
839#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000840}
841
842/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000843 * Generate an ISN_PUSHJOB instruction. Job is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000844 */
845 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000846generate_PUSHJOB(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000847{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000848 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100849#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100850 if (generate_instr_type(cctx, ISN_PUSHJOB, &t_job) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000851 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000852 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100853#else
854 emsg(_(e_channel_job_feature_not_available));
855 return FAIL;
856#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000857}
858
859/*
860 * Generate an ISN_PUSHBLOB instruction.
861 * Consumes "blob".
862 */
863 int
864generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
865{
866 isn_T *isn;
867
868 RETURN_OK_IF_SKIP(cctx);
869 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
870 return FAIL;
871 isn->isn_arg.blob = blob;
872
873 return OK;
874}
875
876/*
877 * Generate an ISN_PUSHFUNC instruction with name "name".
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100878 * When "may_prefix" is TRUE prefix "g:" unless "name" is script-local or
879 * autoload.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000880 */
881 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100882generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type, int may_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000883{
884 isn_T *isn;
885 char_u *funcname;
886
887 RETURN_OK_IF_SKIP(cctx);
888 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
889 return FAIL;
890 if (name == NULL)
891 funcname = NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100892 else if (!may_prefix
893 || *name == K_SPECIAL // script-local
Bram Moolenaard041f422022-01-12 19:54:00 +0000894 || vim_strchr(name, AUTOLOAD_CHAR) != NULL) // autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000895 funcname = vim_strsave(name);
896 else
897 {
898 funcname = alloc(STRLEN(name) + 3);
899 if (funcname != NULL)
900 {
901 STRCPY(funcname, "g:");
902 STRCPY(funcname + 2, name);
903 }
904 }
905
906 isn->isn_arg.string = funcname;
907 return OK;
908}
909
910/*
Bram Moolenaar06b77222022-01-25 15:51:56 +0000911 * Generate an ISN_AUTOLOAD instruction.
912 */
913 int
914generate_AUTOLOAD(cctx_T *cctx, char_u *name, type_T *type)
915{
916 isn_T *isn;
917
918 RETURN_OK_IF_SKIP(cctx);
919 if ((isn = generate_instr_type(cctx, ISN_AUTOLOAD, type)) == NULL)
920 return FAIL;
921 isn->isn_arg.string = vim_strsave(name);
922 if (isn->isn_arg.string == NULL)
923 return FAIL;
924 return OK;
925}
926
927/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000928 * Generate an ISN_GETITEM instruction with "index".
929 * "with_op" is TRUE for "+=" and other operators, the stack has the current
930 * value below the list with values.
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100931 * Caller must check the type is a list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000932 */
933 int
934generate_GETITEM(cctx_T *cctx, int index, int with_op)
935{
936 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000937 type_T *type = get_type_on_stack(cctx, with_op ? 1 : 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000938 type_T *item_type = &t_any;
939
940 RETURN_OK_IF_SKIP(cctx);
941
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000942 item_type = type->tt_member;
943 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
944 return FAIL;
945 isn->isn_arg.getitem.gi_index = index;
946 isn->isn_arg.getitem.gi_with_op = with_op;
947
948 // add the item type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +0000949 return push_type_stack(cctx, item_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000950}
951
952/*
953 * Generate an ISN_SLICE instruction with "count".
954 */
955 int
956generate_SLICE(cctx_T *cctx, int count)
957{
958 isn_T *isn;
959
960 RETURN_OK_IF_SKIP(cctx);
961 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
962 return FAIL;
963 isn->isn_arg.number = count;
964 return OK;
965}
966
967/*
968 * Generate an ISN_CHECKLEN instruction with "min_len".
969 */
970 int
971generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
972{
973 isn_T *isn;
974
975 RETURN_OK_IF_SKIP(cctx);
976
977 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
978 return FAIL;
979 isn->isn_arg.checklen.cl_min_len = min_len;
980 isn->isn_arg.checklen.cl_more_OK = more_OK;
981
982 return OK;
983}
984
985/*
986 * Generate an ISN_STORE instruction.
987 */
988 int
989generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
990{
991 isn_T *isn;
992
993 RETURN_OK_IF_SKIP(cctx);
994 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
995 return FAIL;
996 if (name != NULL)
997 isn->isn_arg.string = vim_strsave(name);
998 else
999 isn->isn_arg.number = idx;
1000
1001 return OK;
1002}
1003
1004/*
Bram Moolenaard505d172022-12-18 21:42:55 +00001005 * Generate an ISN_LOAD_CLASSMEMBER ("load" == TRUE) or ISN_STORE_CLASSMEMBER
1006 * ("load" == FALSE) instruction.
1007 */
1008 int
1009generate_CLASSMEMBER(
1010 cctx_T *cctx,
1011 int load,
1012 class_T *cl,
1013 int idx)
1014{
1015 isn_T *isn;
1016
1017 RETURN_OK_IF_SKIP(cctx);
1018 if (load)
1019 {
1020 ocmember_T *m = &cl->class_class_members[idx];
1021 isn = generate_instr_type(cctx, ISN_LOAD_CLASSMEMBER, m->ocm_type);
1022 }
1023 else
1024 {
1025 isn = generate_instr_drop(cctx, ISN_STORE_CLASSMEMBER, 1);
1026 }
1027 if (isn == NULL)
1028 return FAIL;
1029 isn->isn_arg.classmember.cm_class = cl;
1030 ++cl->class_refcount;
1031 isn->isn_arg.classmember.cm_idx = idx;
1032
1033 return OK;
1034}
1035
1036/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001037 * Generate an ISN_STOREOUTER instruction.
1038 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +00001039 static int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001040generate_STOREOUTER(cctx_T *cctx, int idx, int level, int loop_idx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001041{
1042 isn_T *isn;
1043
1044 RETURN_OK_IF_SKIP(cctx);
1045 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1046 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001047 if (level == 1 && loop_idx >= 0 && idx >= loop_idx)
1048 {
1049 // Store a variable defined in a loop. A copy will be made at the end
1050 // of the loop. TODO: how about deeper nesting?
1051 isn->isn_arg.outer.outer_idx = idx - loop_idx;
1052 isn->isn_arg.outer.outer_depth = OUTER_LOOP_DEPTH;
1053 }
1054 else
1055 {
1056 isn->isn_arg.outer.outer_idx = idx;
1057 isn->isn_arg.outer.outer_depth = level;
1058 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001059
1060 return OK;
1061}
1062
1063/*
1064 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1065 */
1066 int
1067generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1068{
1069 isn_T *isn;
1070
1071 RETURN_OK_IF_SKIP(cctx);
1072 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1073 return FAIL;
1074 isn->isn_arg.storenr.stnr_idx = idx;
1075 isn->isn_arg.storenr.stnr_val = value;
1076
1077 return OK;
1078}
1079
1080/*
1081 * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction
1082 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +00001083 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001084generate_STOREOPT(
1085 cctx_T *cctx,
1086 isntype_T isn_type,
1087 char_u *name,
1088 int opt_flags)
1089{
1090 isn_T *isn;
1091
1092 RETURN_OK_IF_SKIP(cctx);
1093 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1094 return FAIL;
1095 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1096 isn->isn_arg.storeopt.so_flags = opt_flags;
1097
1098 return OK;
1099}
1100
1101/*
1102 * Generate an ISN_LOAD or similar instruction.
1103 */
1104 int
1105generate_LOAD(
1106 cctx_T *cctx,
1107 isntype_T isn_type,
1108 int idx,
1109 char_u *name,
1110 type_T *type)
1111{
1112 isn_T *isn;
1113
1114 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001115 if ((isn = generate_instr_type2(cctx, isn_type, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001116 return FAIL;
1117 if (name != NULL)
1118 isn->isn_arg.string = vim_strsave(name);
1119 else
1120 isn->isn_arg.number = idx;
1121
1122 return OK;
1123}
1124
1125/*
1126 * Generate an ISN_LOADOUTER instruction
1127 */
1128 int
1129generate_LOADOUTER(
1130 cctx_T *cctx,
1131 int idx,
1132 int nesting,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001133 int loop_depth,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001134 int loop_idx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001135 type_T *type)
1136{
1137 isn_T *isn;
1138
1139 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001140 if ((isn = generate_instr_type2(cctx, ISN_LOADOUTER, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001141 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001142 if (nesting == 1 && loop_idx >= 0 && idx >= loop_idx)
1143 {
1144 // Load a variable defined in a loop. A copy will be made at the end
Bram Moolenaarcc341812022-09-19 15:54:34 +01001145 // of the loop.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001146 isn->isn_arg.outer.outer_idx = idx - loop_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001147 isn->isn_arg.outer.outer_depth = -loop_depth - 1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001148 }
1149 else
1150 {
1151 isn->isn_arg.outer.outer_idx = idx;
1152 isn->isn_arg.outer.outer_depth = nesting;
1153 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001154
1155 return OK;
1156}
1157
1158/*
1159 * Generate an ISN_LOADV instruction for v:var.
1160 */
1161 int
1162generate_LOADV(
1163 cctx_T *cctx,
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001164 char_u *name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001165{
1166 int di_flags;
1167 int vidx = find_vim_var(name, &di_flags);
1168 type_T *type;
1169
1170 RETURN_OK_IF_SKIP(cctx);
1171 if (vidx < 0)
1172 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001173 semsg(_(e_variable_not_found_str), name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001174 return FAIL;
1175 }
Bram Moolenaard787e402021-12-24 21:36:12 +00001176 type = get_vim_var_type(vidx, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001177 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
1178}
1179
1180/*
1181 * Generate an ISN_UNLET instruction.
1182 */
1183 int
1184generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
1185{
1186 isn_T *isn;
1187
1188 RETURN_OK_IF_SKIP(cctx);
1189 if ((isn = generate_instr(cctx, isn_type)) == NULL)
1190 return FAIL;
1191 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1192 isn->isn_arg.unlet.ul_forceit = forceit;
1193
1194 return OK;
1195}
1196
1197/*
1198 * Generate an ISN_LOCKCONST instruction.
1199 */
1200 int
1201generate_LOCKCONST(cctx_T *cctx)
1202{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001203 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01001204 if (generate_instr(cctx, ISN_LOCKCONST) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001205 return FAIL;
1206 return OK;
1207}
1208
1209/*
1210 * Generate an ISN_LOADS instruction.
1211 */
1212 int
1213generate_OLDSCRIPT(
1214 cctx_T *cctx,
1215 isntype_T isn_type,
1216 char_u *name,
1217 int sid,
1218 type_T *type)
1219{
1220 isn_T *isn;
1221
1222 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001223 if (isn_type == ISN_LOADS || isn_type == ISN_LOADEXPORT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001224 isn = generate_instr_type(cctx, isn_type, type);
1225 else
1226 isn = generate_instr_drop(cctx, isn_type, 1);
1227 if (isn == NULL)
1228 return FAIL;
1229 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1230 isn->isn_arg.loadstore.ls_sid = sid;
1231
1232 return OK;
1233}
1234
1235/*
1236 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1237 */
1238 int
1239generate_VIM9SCRIPT(
1240 cctx_T *cctx,
1241 isntype_T isn_type,
1242 int sid,
1243 int idx,
1244 type_T *type)
1245{
1246 isn_T *isn;
1247 scriptref_T *sref;
1248 scriptitem_T *si = SCRIPT_ITEM(sid);
1249
1250 RETURN_OK_IF_SKIP(cctx);
1251 if (isn_type == ISN_LOADSCRIPT)
Bram Moolenaar160afdb2022-02-06 17:17:02 +00001252 isn = generate_instr_type2(cctx, isn_type, type, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001253 else
1254 isn = generate_instr_drop(cctx, isn_type, 1);
1255 if (isn == NULL)
1256 return FAIL;
1257
1258 // This requires three arguments, which doesn't fit in an instruction, thus
1259 // we need to allocate a struct for this.
1260 sref = ALLOC_ONE(scriptref_T);
1261 if (sref == NULL)
1262 return FAIL;
1263 isn->isn_arg.script.scriptref = sref;
1264 sref->sref_sid = sid;
1265 sref->sref_idx = idx;
1266 sref->sref_seq = si->sn_script_seq;
1267 sref->sref_type = type;
1268 return OK;
1269}
1270
1271/*
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001272 * Generate an ISN_NEWLIST instruction for "count" items.
1273 * "use_null" is TRUE for null_list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001274 */
1275 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001276generate_NEWLIST(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001277{
1278 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001279 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001280 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001281 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001282
1283 RETURN_OK_IF_SKIP(cctx);
1284 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1285 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001286 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001287
Bram Moolenaar078a4612022-01-04 15:17:03 +00001288 // Get the member type and the declared member type from all the items on
1289 // the stack.
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001290 member_type = get_member_type_from_stack(count, 1, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001291 type = get_list_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001292 decl_type = get_list_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001293
1294 // drop the value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001295 cctx->ctx_type_stack.ga_len -= count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001296
1297 // add the list type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001298 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001299}
1300
1301/*
1302 * Generate an ISN_NEWDICT instruction.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001303 * "use_null" is TRUE for null_dict.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001304 */
1305 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001306generate_NEWDICT(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001307{
1308 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001309 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001310 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001311 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001312
1313 RETURN_OK_IF_SKIP(cctx);
1314 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1315 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001316 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001317
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001318 member_type = get_member_type_from_stack(count, 2, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001319 type = get_dict_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001320 decl_type = get_dict_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001321
1322 // drop the key and value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001323 cctx->ctx_type_stack.ga_len -= 2 * count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001324
1325 // add the dict type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001326 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001327}
1328
1329/*
1330 * Generate an ISN_FUNCREF instruction.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001331 * "isnp" is set to the instruction, so that fr_dfunc_idx can be set later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001332 */
1333 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001334generate_FUNCREF(
1335 cctx_T *cctx,
1336 ufunc_T *ufunc,
1337 isn_T **isnp)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001338{
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001339 isn_T *isn;
1340 type_T *type;
1341 funcref_extra_T *extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001342 loopvarinfo_T loopinfo;
1343 int has_vars;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001344
1345 RETURN_OK_IF_SKIP(cctx);
1346 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1347 return FAIL;
Bram Moolenaara915fa02022-03-23 11:29:15 +00001348 if (isnp != NULL)
1349 *isnp = isn;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001350
Bram Moolenaarcc341812022-09-19 15:54:34 +01001351 has_vars = get_loop_var_info(cctx, &loopinfo);
1352 if (ufunc->uf_def_status == UF_NOT_COMPILED || has_vars)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001353 {
1354 extra = ALLOC_CLEAR_ONE(funcref_extra_T);
1355 if (extra == NULL)
1356 return FAIL;
1357 isn->isn_arg.funcref.fr_extra = extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001358 extra->fre_loopvar_info = loopinfo;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001359 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001360 if (ufunc->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001361 extra->fre_func_name = vim_strsave(ufunc->uf_name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001362 else
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001363 {
1364 if (isnp == NULL && ufunc->uf_def_status == UF_TO_BE_COMPILED)
1365 // compile the function now, we need the uf_dfunc_idx value
1366 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001367 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001368 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01001369
1370 // Reserve an extra variable to keep track of the number of closures
1371 // created.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001372 cctx->ctx_has_closure = 1;
1373
1374 // If the referenced function is a closure, it may use items further up in
Bram Moolenaar139575d2022-03-15 19:29:30 +00001375 // the nested context, including this one. But not a function defined at
1376 // the script level.
1377 if ((ufunc->uf_flags & FC_CLOSURE)
1378 && func_name_refcount(cctx->ctx_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001379 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1380
Bram Moolenaar078a4612022-01-04 15:17:03 +00001381 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1382 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001383}
1384
1385/*
1386 * Generate an ISN_NEWFUNC instruction.
1387 * "lambda_name" and "func_name" must be in allocated memory and will be
1388 * consumed.
1389 */
1390 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001391generate_NEWFUNC(
1392 cctx_T *cctx,
1393 char_u *lambda_name,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001394 char_u *func_name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001395{
1396 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001397 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001398
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001399 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001400 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001401 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1402 ret = FAIL;
1403 else
1404 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001405 newfuncarg_T *arg = ALLOC_CLEAR_ONE(newfuncarg_T);
1406
1407 if (arg == NULL)
1408 ret = FAIL;
1409 else
1410 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01001411 // Reserve an extra variable to keep track of the number of
1412 // closures created.
1413 cctx->ctx_has_closure = 1;
1414
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001415 isn->isn_arg.newfunc.nf_arg = arg;
1416 arg->nfa_lambda = lambda_name;
1417 arg->nfa_global = func_name;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001418 (void)get_loop_var_info(cctx, &arg->nfa_loopvar_info);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001419 return OK;
1420 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001421 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001422 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001423 vim_free(lambda_name);
1424 vim_free(func_name);
1425 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001426}
1427
1428/*
1429 * Generate an ISN_DEF instruction: list functions
1430 */
1431 int
1432generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1433{
1434 isn_T *isn;
1435
1436 RETURN_OK_IF_SKIP(cctx);
1437 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1438 return FAIL;
1439 if (len > 0)
1440 {
1441 isn->isn_arg.string = vim_strnsave(name, len);
1442 if (isn->isn_arg.string == NULL)
1443 return FAIL;
1444 }
1445 return OK;
1446}
1447
1448/*
1449 * Generate an ISN_JUMP instruction.
1450 */
1451 int
1452generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1453{
1454 isn_T *isn;
1455 garray_T *stack = &cctx->ctx_type_stack;
1456
1457 RETURN_OK_IF_SKIP(cctx);
1458 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1459 return FAIL;
1460 isn->isn_arg.jump.jump_when = when;
1461 isn->isn_arg.jump.jump_where = where;
1462
1463 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1464 --stack->ga_len;
1465
1466 return OK;
1467}
1468
1469/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001470 * Generate an ISN_WHILE instruction. Similar to ISN_JUMP for :while
1471 */
1472 int
1473generate_WHILE(cctx_T *cctx, int funcref_idx)
1474{
1475 isn_T *isn;
1476 garray_T *stack = &cctx->ctx_type_stack;
1477
1478 RETURN_OK_IF_SKIP(cctx);
1479 if ((isn = generate_instr(cctx, ISN_WHILE)) == NULL)
1480 return FAIL;
1481 isn->isn_arg.whileloop.while_funcref_idx = funcref_idx;
1482 isn->isn_arg.whileloop.while_end = 0; // filled in later
1483
1484 if (stack->ga_len > 0)
1485 --stack->ga_len;
1486
1487 return OK;
1488}
1489
1490/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001491 * Generate an ISN_JUMP_IF_ARG_SET or ISN_JUMP_IF_ARG_NOT_SET instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001492 */
1493 int
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001494generate_JUMP_IF_ARG(cctx_T *cctx, isntype_T isn_type, int arg_off)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001495{
1496 isn_T *isn;
1497
1498 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001499 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001500 return FAIL;
1501 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1502 // jump_where is set later
1503 return OK;
1504}
1505
1506 int
1507generate_FOR(cctx_T *cctx, int loop_idx)
1508{
1509 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001510
1511 RETURN_OK_IF_SKIP(cctx);
1512 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1513 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001514 isn->isn_arg.forloop.for_loop_idx = loop_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001515
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001516 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001517 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001518}
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001519
1520 int
Bram Moolenaarcc341812022-09-19 15:54:34 +01001521generate_ENDLOOP(cctx_T *cctx, loop_info_T *loop_info)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001522{
1523 isn_T *isn;
1524
1525 RETURN_OK_IF_SKIP(cctx);
1526 if ((isn = generate_instr(cctx, ISN_ENDLOOP)) == NULL)
1527 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001528 isn->isn_arg.endloop.end_depth = loop_info->li_depth;
1529 isn->isn_arg.endloop.end_funcref_idx = loop_info->li_funcref_idx;
1530 isn->isn_arg.endloop.end_var_idx = loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001531 isn->isn_arg.endloop.end_var_count =
Bram Moolenaarcc341812022-09-19 15:54:34 +01001532 cctx->ctx_locals.ga_len - loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001533 return OK;
1534}
1535
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001536/*
1537 * Generate an ISN_TRYCONT instruction.
1538 */
1539 int
1540generate_TRYCONT(cctx_T *cctx, int levels, int where)
1541{
1542 isn_T *isn;
1543
1544 RETURN_OK_IF_SKIP(cctx);
1545 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1546 return FAIL;
1547 isn->isn_arg.trycont.tct_levels = levels;
1548 isn->isn_arg.trycont.tct_where = where;
1549
1550 return OK;
1551}
1552
Bram Moolenaar16900322022-09-08 19:51:45 +01001553/*
1554 * Check "argount" arguments and their types on the type stack.
1555 * Give an error and return FAIL if something is wrong.
1556 * When "method_call" is NULL no code is generated.
1557 */
1558 int
1559check_internal_func_args(
1560 cctx_T *cctx,
1561 int func_idx,
1562 int argcount,
1563 int method_call,
1564 type2_T **argtypes,
1565 type2_T *shuffled_argtypes)
1566{
1567 garray_T *stack = &cctx->ctx_type_stack;
1568 int argoff = check_internal_func(func_idx, argcount);
1569
1570 if (argoff < 0)
1571 return FAIL;
1572
1573 if (method_call && argoff > 1)
1574 {
1575 isn_T *isn = generate_instr(cctx, ISN_SHUFFLE);
1576
1577 if (isn == NULL)
1578 return FAIL;
1579 isn->isn_arg.shuffle.shfl_item = argcount;
1580 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1581 }
1582
1583 if (argcount > 0)
1584 {
1585 type2_T *typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
1586
1587 // Check the types of the arguments.
1588 if (method_call && argoff > 1)
1589 {
1590 int i;
1591
1592 for (i = 0; i < argcount; ++i)
1593 shuffled_argtypes[i] = (i < argoff - 1)
1594 ? typep[i + 1]
1595 : (i == argoff - 1) ? typep[0] : typep[i];
1596 *argtypes = shuffled_argtypes;
1597 }
1598 else
1599 {
1600 int i;
1601
1602 for (i = 0; i < argcount; ++i)
1603 shuffled_argtypes[i] = typep[i];
1604 *argtypes = shuffled_argtypes;
1605 }
1606 if (internal_func_check_arg_types(*argtypes, func_idx, argcount,
1607 cctx) == FAIL)
1608 return FAIL;
1609 }
1610 return OK;
1611}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001612
1613/*
1614 * Generate an ISN_BCALL instruction.
1615 * "method_call" is TRUE for "value->method()"
1616 * Return FAIL if the number of arguments is wrong.
1617 */
1618 int
1619generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1620{
1621 isn_T *isn;
1622 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001623 type2_T *argtypes = NULL;
1624 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1625 type2_T *maptype = NULL;
1626 type_T *type;
Bram Moolenaar81330182022-02-01 12:11:58 +00001627 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001628
1629 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar16900322022-09-08 19:51:45 +01001630
1631 if (check_internal_func_args(cctx, func_idx, argcount, method_call,
1632 &argtypes, shuffled_argtypes) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001633 return FAIL;
1634
Bram Moolenaar16900322022-09-08 19:51:45 +01001635 if (internal_func_is_map(func_idx))
1636 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001637
1638 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1639 return FAIL;
1640 isn->isn_arg.bfunc.cbf_idx = func_idx;
1641 isn->isn_arg.bfunc.cbf_argcount = argcount;
1642
1643 // Drop the argument types and push the return type.
1644 stack->ga_len -= argcount;
Bram Moolenaar32517c42023-01-15 18:17:12 +00001645 type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type,
1646 cctx->ctx_type_list);
Bram Moolenaar81330182022-02-01 12:11:58 +00001647 if (push_type_stack2(cctx, type, decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001648 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001649
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001650 if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
1651 && maptype[0].type_decl->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001652 // Check that map() didn't change the item types.
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001653 generate_TYPECHECK(cctx, maptype[0].type_decl, FALSE, -1, FALSE, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001654
1655 return OK;
1656}
1657
1658/*
1659 * Generate an ISN_LISTAPPEND instruction. Works like add().
1660 * Argument count is already checked.
1661 */
1662 int
1663generate_LISTAPPEND(cctx_T *cctx)
1664{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001665 type_T *list_type;
1666 type_T *item_type;
1667 type_T *expected;
1668
1669 // Caller already checked that list_type is a list.
Bram Moolenaar81330182022-02-01 12:11:58 +00001670 // For checking the item type we use the declared type of the list and the
1671 // current type of the added item, adding a string to [1, 2] is OK.
1672 list_type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaarfa103972022-09-29 19:14:42 +01001673 if (arg_type_modifiable(list_type, 1) == FAIL)
1674 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001675 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001676 expected = list_type->tt_member;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001677 if (need_type(item_type, expected, FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001678 return FAIL;
1679
1680 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1681 return FAIL;
1682
Bram Moolenaar078a4612022-01-04 15:17:03 +00001683 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001684 return OK;
1685}
1686
1687/*
1688 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1689 * Argument count is already checked.
1690 */
1691 int
1692generate_BLOBAPPEND(cctx_T *cctx)
1693{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001694 type_T *item_type;
1695
Bram Moolenaarfa103972022-09-29 19:14:42 +01001696 // Caller already checked that blob_type is a blob, check it is modifiable.
1697 if (arg_type_modifiable(get_decl_type_on_stack(cctx, 1), 1) == FAIL)
1698 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001699 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001700 if (need_type(item_type, &t_number, FALSE,
1701 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001702 return FAIL;
1703
1704 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1705 return FAIL;
1706
Bram Moolenaar078a4612022-01-04 15:17:03 +00001707 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001708 return OK;
1709}
1710
1711/*
Bram Moolenaard0200c82023-01-28 15:19:40 +00001712 * Generate an ISN_DCALL, ISN_UCALL or ISN_METHODCALL instruction.
1713 * When calling a method on an object, of which we know the interface only,
1714 * then "cl" is the interface and "mi" the method index on the interface.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001715 * Return FAIL if the number of arguments is wrong.
1716 */
1717 int
Bram Moolenaard0200c82023-01-28 15:19:40 +00001718generate_CALL(
1719 cctx_T *cctx,
1720 ufunc_T *ufunc,
1721 class_T *cl,
1722 int mi,
1723 int pushed_argcount)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001724{
1725 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001726 int regular_args = ufunc->uf_args.ga_len;
1727 int argcount = pushed_argcount;
1728
1729 RETURN_OK_IF_SKIP(cctx);
1730 if (argcount > regular_args && !has_varargs(ufunc))
1731 {
1732 semsg(_(e_too_many_arguments_for_function_str),
1733 printable_func_name(ufunc));
1734 return FAIL;
1735 }
1736 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1737 {
1738 semsg(_(e_not_enough_arguments_for_function_str),
1739 printable_func_name(ufunc));
1740 return FAIL;
1741 }
1742
1743 if (ufunc->uf_def_status != UF_NOT_COMPILED
1744 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1745 {
1746 int i;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001747 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001748
1749 for (i = 0; i < argcount; ++i)
1750 {
1751 type_T *expected;
1752 type_T *actual;
1753
Bram Moolenaar078a4612022-01-04 15:17:03 +00001754 actual = get_type_on_stack(cctx, argcount - i - 1);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001755 if (actual->tt_type == VAR_SPECIAL
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001756 && i >= regular_args - ufunc->uf_def_args.ga_len)
1757 {
1758 // assume v:none used for default argument value
1759 continue;
1760 }
1761 if (i < regular_args)
1762 {
1763 if (ufunc->uf_arg_types == NULL)
1764 continue;
1765 expected = ufunc->uf_arg_types[i];
1766 }
1767 else if (ufunc->uf_va_type == NULL
1768 || ufunc->uf_va_type == &t_list_any)
1769 // possibly a lambda or "...: any"
1770 expected = &t_any;
1771 else
1772 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001773 if (need_type(actual, expected, FALSE,
1774 -argcount + i, i + 1, cctx, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001775 {
1776 arg_type_mismatch(expected, actual, i + 1);
1777 return FAIL;
1778 }
1779 }
Bram Moolenaar139575d2022-03-15 19:29:30 +00001780 compile_type = get_compile_type(ufunc);
1781 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001782 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaar139575d2022-03-15 19:29:30 +00001783 compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001784 return FAIL;
1785 }
1786 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1787 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001788 emsg_funcname(e_call_to_function_that_failed_to_compile_str,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001789 ufunc->uf_name);
1790 return FAIL;
1791 }
1792
Bram Moolenaard0200c82023-01-28 15:19:40 +00001793 if ((isn = generate_instr(cctx, cl != NULL ? ISN_METHODCALL
1794 : ufunc->uf_def_status != UF_NOT_COMPILED
1795 ? ISN_DCALL : ISN_UCALL)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001796 return FAIL;
Bram Moolenaard0200c82023-01-28 15:19:40 +00001797 if (isn->isn_type == ISN_METHODCALL)
1798 {
1799 isn->isn_arg.mfunc = ALLOC_ONE(cmfunc_T);
1800 if (isn->isn_arg.mfunc == NULL)
1801 return FAIL;
1802 isn->isn_arg.mfunc->cmf_itf = cl;
1803 ++cl->class_refcount;
1804 isn->isn_arg.mfunc->cmf_idx = mi;
1805 isn->isn_arg.mfunc->cmf_argcount = argcount;
1806 }
1807 else if (isn->isn_type == ISN_DCALL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001808 {
1809 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1810 isn->isn_arg.dfunc.cdf_argcount = argcount;
1811 }
1812 else
1813 {
1814 // A user function may be deleted and redefined later, can't use the
1815 // ufunc pointer, need to look it up again at runtime.
1816 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1817 isn->isn_arg.ufunc.cuf_argcount = argcount;
1818 }
1819
Bram Moolenaar078a4612022-01-04 15:17:03 +00001820 // drop the argument types
1821 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001822
Bram Moolenaar078a4612022-01-04 15:17:03 +00001823 // add return type
1824 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001825}
1826
1827/*
1828 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1829 */
1830 int
1831generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1832{
1833 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001834
1835 RETURN_OK_IF_SKIP(cctx);
1836 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1837 return FAIL;
1838 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1839 isn->isn_arg.ufunc.cuf_argcount = argcount;
1840
Bram Moolenaar078a4612022-01-04 15:17:03 +00001841 // drop the argument types
1842 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001843
Bram Moolenaar078a4612022-01-04 15:17:03 +00001844 // add return value
1845 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001846}
1847
1848/*
Bram Moolenaar16900322022-09-08 19:51:45 +01001849 * Check the arguments of function "type" against the types on the stack.
1850 * Returns OK or FAIL;
1851 */
1852 int
1853check_func_args_from_type(
1854 cctx_T *cctx,
1855 type_T *type,
1856 int argcount,
1857 int at_top,
1858 char_u *name)
1859{
1860 if (type->tt_argcount != -1)
1861 {
1862 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1863
1864 if (argcount < type->tt_min_argcount - varargs)
1865 {
1866 emsg_funcname(e_not_enough_arguments_for_function_str, name);
1867 return FAIL;
1868 }
1869 if (!varargs && argcount > type->tt_argcount)
1870 {
1871 emsg_funcname(e_too_many_arguments_for_function_str, name);
1872 return FAIL;
1873 }
1874 if (type->tt_args != NULL)
1875 {
1876 int i;
1877
1878 for (i = 0; i < argcount; ++i)
1879 {
1880 int offset = -argcount + i - (at_top ? 0 : 1);
1881 type_T *actual = get_type_on_stack(cctx, -1 - offset);
1882 type_T *expected;
1883
1884 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaar36818a92023-01-03 12:33:26 +00001885 {
1886 expected = type->tt_args[type->tt_argcount - 1];
1887 if (expected != NULL && expected->tt_type == VAR_LIST)
1888 expected = expected->tt_member;
1889 if (expected == NULL)
1890 expected = &t_any;
1891 }
Bram Moolenaar16900322022-09-08 19:51:45 +01001892 else if (i >= type->tt_min_argcount
1893 && actual->tt_type == VAR_SPECIAL)
1894 expected = &t_any;
1895 else
1896 expected = type->tt_args[i];
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001897 if (need_type(actual, expected, FALSE,
1898 offset, i + 1, cctx, TRUE, FALSE) == FAIL)
Bram Moolenaar16900322022-09-08 19:51:45 +01001899 {
1900 arg_type_mismatch(expected, actual, i + 1);
1901 return FAIL;
1902 }
1903 }
1904 }
1905 }
1906
1907 return OK;
1908}
1909/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001910 * Generate an ISN_PCALL instruction.
1911 * "type" is the type of the FuncRef.
1912 */
1913 int
1914generate_PCALL(
1915 cctx_T *cctx,
1916 int argcount,
1917 char_u *name,
1918 type_T *type,
1919 int at_top)
1920{
1921 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001922 type_T *ret_type;
1923
1924 RETURN_OK_IF_SKIP(cctx);
1925
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001926 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001927 ret_type = &t_any;
1928 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1929 {
Bram Moolenaar16900322022-09-08 19:51:45 +01001930 if (check_func_args_from_type(cctx, type, argcount, at_top, name) == FAIL)
1931 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001932
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001933 ret_type = type->tt_member;
1934 if (ret_type == &t_unknown)
1935 // return type not known yet, use a runtime check
1936 ret_type = &t_any;
1937 }
1938 else
1939 {
1940 semsg(_(e_not_callable_type_str), name);
1941 return FAIL;
1942 }
1943
1944 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1945 return FAIL;
1946 isn->isn_arg.pfunc.cpf_top = at_top;
1947 isn->isn_arg.pfunc.cpf_argcount = argcount;
1948
Bram Moolenaar078a4612022-01-04 15:17:03 +00001949 // drop the arguments and the funcref/partial
1950 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001951
Bram Moolenaar078a4612022-01-04 15:17:03 +00001952 // push the return value
1953 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001954
1955 // If partial is above the arguments it must be cleared and replaced with
1956 // the return value.
1957 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1958 return FAIL;
1959
1960 return OK;
1961}
1962
1963/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001964 * Generate an ISN_DEFER instruction.
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001965 * "obj_method" is one for "obj.Method()", zero otherwise.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001966 */
1967 int
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001968generate_DEFER(cctx_T *cctx, int var_idx, int obj_method, int argcount)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001969{
1970 isn_T *isn;
1971
1972 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001973 if ((isn = generate_instr_drop(cctx,
1974 obj_method == 0 ? ISN_DEFER : ISN_DEFEROBJ,
1975 argcount + 1)) == NULL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001976 return FAIL;
1977 isn->isn_arg.defer.defer_var_idx = var_idx;
1978 isn->isn_arg.defer.defer_argcount = argcount;
1979 return OK;
1980}
1981
1982/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001983 * Generate an ISN_STRINGMEMBER instruction.
1984 */
1985 int
1986generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
1987{
1988 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001989 type_T *type;
1990
1991 RETURN_OK_IF_SKIP(cctx);
1992 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
1993 return FAIL;
1994 isn->isn_arg.string = vim_strnsave(name, len);
1995
1996 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00001997 type = get_type_on_stack(cctx, 0);
Bram Moolenaar0089ce22022-10-08 14:39:36 +01001998 if (type->tt_type != VAR_DICT
1999 && type->tt_type != VAR_ANY && type->tt_type != VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002000 {
2001 char *tofree;
2002
2003 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
2004 name, type_name(type, &tofree));
2005 vim_free(tofree);
2006 return FAIL;
2007 }
2008 // change dict type to dict member type
2009 if (type->tt_type == VAR_DICT)
2010 {
Bram Moolenaar0089ce22022-10-08 14:39:36 +01002011 type_T *ntype = type->tt_member->tt_type == VAR_UNKNOWN
Bram Moolenaar078a4612022-01-04 15:17:03 +00002012 ? &t_any : type->tt_member;
2013 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002014 }
2015
2016 return OK;
2017}
2018
2019/*
2020 * Generate an ISN_ECHO instruction.
2021 */
2022 int
2023generate_ECHO(cctx_T *cctx, int with_white, int count)
2024{
2025 isn_T *isn;
2026
2027 RETURN_OK_IF_SKIP(cctx);
2028 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2029 return FAIL;
2030 isn->isn_arg.echo.echo_with_white = with_white;
2031 isn->isn_arg.echo.echo_count = count;
2032
2033 return OK;
2034}
2035
2036/*
2037 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
2038 */
2039 int
2040generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
2041{
2042 isn_T *isn;
2043
Bram Moolenaar2eae3d22022-10-07 15:09:27 +01002044 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002045 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
2046 return FAIL;
2047 isn->isn_arg.number = count;
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002048 return OK;
2049}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002050
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002051/*
2052 * Generate an ISN_ECHOWINDOW instruction
2053 */
2054 int
2055generate_ECHOWINDOW(cctx_T *cctx, int count, long time)
2056{
2057 isn_T *isn;
2058
2059 if ((isn = generate_instr_drop(cctx, ISN_ECHOWINDOW, count)) == NULL)
2060 return FAIL;
2061 isn->isn_arg.echowin.ewin_count = count;
2062 isn->isn_arg.echowin.ewin_time = time;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002063 return OK;
2064}
2065
2066/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002067 * Generate an ISN_SOURCE instruction.
2068 */
2069 int
2070generate_SOURCE(cctx_T *cctx, int sid)
2071{
2072 isn_T *isn;
2073
2074 if ((isn = generate_instr(cctx, ISN_SOURCE)) == NULL)
2075 return FAIL;
2076 isn->isn_arg.number = sid;
2077
2078 return OK;
2079}
2080
2081/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002082 * Generate an ISN_PUT instruction.
2083 */
2084 int
2085generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2086{
2087 isn_T *isn;
2088
2089 RETURN_OK_IF_SKIP(cctx);
2090 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2091 return FAIL;
2092 isn->isn_arg.put.put_regname = regname;
2093 isn->isn_arg.put.put_lnum = lnum;
2094 return OK;
2095}
2096
2097/*
2098 * Generate an EXEC instruction that takes a string argument.
2099 * A copy is made of "line".
2100 */
2101 int
2102generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
2103{
2104 isn_T *isn;
2105
2106 RETURN_OK_IF_SKIP(cctx);
2107 if ((isn = generate_instr(cctx, isntype)) == NULL)
2108 return FAIL;
2109 isn->isn_arg.string = vim_strsave(line);
2110 return OK;
2111}
2112
2113/*
2114 * Generate an EXEC instruction that takes a string argument.
2115 * "str" must be allocated, it is consumed.
2116 */
2117 int
2118generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
2119{
2120 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002121 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002122
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002123 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002124 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002125 if ((isn = generate_instr(cctx, isntype)) == NULL)
2126 ret = FAIL;
2127 else
2128 {
2129 isn->isn_arg.string = str;
2130 return OK;
2131 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002132 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002133 vim_free(str);
2134 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002135}
2136
2137 int
2138generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2139{
2140 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002141
2142 RETURN_OK_IF_SKIP(cctx);
2143 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2144 return FAIL;
2145 isn->isn_arg.string = vim_strsave(line);
2146
Bram Moolenaar078a4612022-01-04 15:17:03 +00002147 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002148}
2149
2150 int
2151generate_EXECCONCAT(cctx_T *cctx, int count)
2152{
2153 isn_T *isn;
2154
2155 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2156 return FAIL;
2157 isn->isn_arg.number = count;
2158 return OK;
2159}
2160
2161/*
2162 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2163 */
2164 int
2165generate_RANGE(cctx_T *cctx, char_u *range)
2166{
2167 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002168
2169 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2170 return FAIL;
2171 isn->isn_arg.string = range;
2172
Bram Moolenaar078a4612022-01-04 15:17:03 +00002173 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002174}
2175
2176 int
2177generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2178{
2179 isn_T *isn;
2180
2181 RETURN_OK_IF_SKIP(cctx);
2182 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2183 return FAIL;
2184 isn->isn_arg.unpack.unp_count = var_count;
2185 isn->isn_arg.unpack.unp_semicolon = semicolon;
2186 return OK;
2187}
2188
2189/*
2190 * Generate an instruction for any command modifiers.
2191 */
2192 int
2193generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
2194{
2195 isn_T *isn;
2196
2197 if (has_cmdmod(cmod, FALSE))
2198 {
2199 cctx->ctx_has_cmdmod = TRUE;
2200
2201 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
2202 return FAIL;
2203 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2204 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2205 return FAIL;
2206 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
2207 // filter program now belongs to the instruction
2208 cmod->cmod_filter_regmatch.regprog = NULL;
2209 }
2210
2211 return OK;
2212}
2213
2214 int
2215generate_undo_cmdmods(cctx_T *cctx)
2216{
2217 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2218 return FAIL;
2219 cctx->ctx_has_cmdmod = FALSE;
2220 return OK;
2221}
2222
2223/*
2224 * Generate a STORE instruction for "dest", not being "dest_local".
Bram Moolenaard505d172022-12-18 21:42:55 +00002225 * "lhs" might be NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002226 * Return FAIL when out of memory.
2227 */
2228 int
2229generate_store_var(
2230 cctx_T *cctx,
2231 assign_dest_T dest,
2232 int opt_flags,
2233 int vimvaridx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002234 type_T *type,
Bram Moolenaard505d172022-12-18 21:42:55 +00002235 char_u *name,
2236 lhs_T *lhs)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002237{
2238 switch (dest)
2239 {
2240 case dest_option:
2241 return generate_STOREOPT(cctx, ISN_STOREOPT,
2242 skip_option_env_lead(name), opt_flags);
2243 case dest_func_option:
2244 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
2245 skip_option_env_lead(name), opt_flags);
2246 case dest_global:
2247 // include g: with the name, easier to execute that way
2248 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
2249 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
2250 case dest_buffer:
2251 // include b: with the name, easier to execute that way
2252 return generate_STORE(cctx, ISN_STOREB, 0, name);
2253 case dest_window:
2254 // include w: with the name, easier to execute that way
2255 return generate_STORE(cctx, ISN_STOREW, 0, name);
2256 case dest_tab:
2257 // include t: with the name, easier to execute that way
2258 return generate_STORE(cctx, ISN_STORET, 0, name);
2259 case dest_env:
2260 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
2261 case dest_reg:
2262 return generate_STORE(cctx, ISN_STOREREG,
2263 name[1] == '@' ? '"' : name[1], NULL);
2264 case dest_vimvar:
2265 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
2266 case dest_script:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002267 {
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002268 int scriptvar_idx = lhs->lhs_scriptvar_idx;
2269 int scriptvar_sid = lhs->lhs_scriptvar_sid;
2270 if (scriptvar_idx < 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002271 {
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002272 isntype_T isn_type = ISN_STORES;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002273
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002274 if (SCRIPT_ID_VALID(scriptvar_sid)
2275 && SCRIPT_ITEM(scriptvar_sid)->sn_import_autoload
2276 && SCRIPT_ITEM(scriptvar_sid)->sn_autoload_prefix
2277 == NULL)
2278 {
2279 // "import autoload './dir/script.vim'" - load script
2280 // first
2281 if (generate_SOURCE(cctx, scriptvar_sid) == FAIL)
2282 return FAIL;
2283 isn_type = ISN_STOREEXPORT;
2284 }
2285
2286 // "s:" may be included in the name.
2287 return generate_OLDSCRIPT(cctx, isn_type, name,
2288 scriptvar_sid, type);
2289 }
2290 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002291 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002292 }
Bram Moolenaard505d172022-12-18 21:42:55 +00002293 case dest_class_member:
2294 return generate_CLASSMEMBER(cctx, FALSE,
2295 lhs->lhs_class, lhs->lhs_classmember_idx);
2296
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002297 case dest_local:
2298 case dest_expr:
2299 // cannot happen
2300 break;
2301 }
2302 return FAIL;
2303}
2304
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002305/*
2306 * Return TRUE when inside a "for" or "while" loop.
2307 */
2308 int
2309inside_loop_scope(cctx_T *cctx)
2310{
2311 scope_T *scope = cctx->ctx_scope;
2312
2313 for (;;)
2314 {
2315 if (scope == NULL)
2316 break;
2317 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
2318 return TRUE;
2319 scope = scope->se_outer;
2320 }
2321 return FALSE;
2322}
2323
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002324 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002325generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002326{
2327 if (lhs->lhs_dest != dest_local)
2328 return generate_store_var(cctx, lhs->lhs_dest,
2329 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
Bram Moolenaard505d172022-12-18 21:42:55 +00002330 lhs->lhs_type, lhs->lhs_name, lhs);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002331
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002332 if (lhs->lhs_lvar == NULL)
2333 return OK;
2334
2335 garray_T *instr = &cctx->ctx_instr;
2336 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2337
2338 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
2339 // ISN_STORENR.
2340 // And "var = 0" does not need any instruction.
2341 if (lhs->lhs_lvar->lv_from_outer == 0
2342 && instr->ga_len == instr_count + 1
2343 && isn->isn_type == ISN_PUSHNR)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002344 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002345 varnumber_T val = isn->isn_arg.number;
2346 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002347
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002348 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002349 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002350 // zero is the default value, no need to do anything
2351 --instr->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002352 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002353 else
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002354 {
2355 isn->isn_type = ISN_STORENR;
2356 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
2357 isn->isn_arg.storenr.stnr_val = val;
2358 }
2359 if (stack->ga_len > 0)
2360 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002361 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002362 else if (lhs->lhs_lvar->lv_from_outer > 0)
2363 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
2364 lhs->lhs_lvar->lv_from_outer, lhs->lhs_lvar->lv_loop_idx);
2365 else
2366 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002367 return OK;
2368}
2369
2370#if defined(FEAT_PROFILE) || defined(PROTO)
2371 void
2372may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2373{
2374 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
2375 generate_instr(cctx, ISN_PROF_END);
2376}
2377#endif
2378
2379
2380/*
2381 * Delete an instruction, free what it contains.
2382 */
2383 void
2384delete_instr(isn_T *isn)
2385{
2386 switch (isn->isn_type)
2387 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002388 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002389 case ISN_DEF:
2390 case ISN_EXEC:
2391 case ISN_EXECRANGE:
2392 case ISN_EXEC_SPLIT:
2393 case ISN_LEGACY_EVAL:
2394 case ISN_LOADAUTO:
2395 case ISN_LOADB:
2396 case ISN_LOADENV:
2397 case ISN_LOADG:
2398 case ISN_LOADOPT:
2399 case ISN_LOADT:
2400 case ISN_LOADW:
2401 case ISN_LOCKUNLOCK:
2402 case ISN_PUSHEXC:
2403 case ISN_PUSHFUNC:
2404 case ISN_PUSHS:
2405 case ISN_RANGE:
2406 case ISN_STOREAUTO:
2407 case ISN_STOREB:
2408 case ISN_STOREENV:
2409 case ISN_STOREG:
2410 case ISN_STORET:
2411 case ISN_STOREW:
2412 case ISN_STRINGMEMBER:
2413 vim_free(isn->isn_arg.string);
2414 break;
2415
2416 case ISN_SUBSTITUTE:
2417 {
2418 int idx;
2419 isn_T *list = isn->isn_arg.subs.subs_instr;
2420
2421 vim_free(isn->isn_arg.subs.subs_cmd);
2422 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2423 delete_instr(list + idx);
2424 vim_free(list);
2425 }
2426 break;
2427
2428 case ISN_INSTR:
2429 {
2430 int idx;
2431 isn_T *list = isn->isn_arg.instr;
2432
2433 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2434 delete_instr(list + idx);
2435 vim_free(list);
2436 }
2437 break;
2438
2439 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002440 case ISN_LOADEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002441 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002442 case ISN_STOREEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002443 vim_free(isn->isn_arg.loadstore.ls_name);
2444 break;
2445
2446 case ISN_UNLET:
2447 case ISN_UNLETENV:
2448 vim_free(isn->isn_arg.unlet.ul_name);
2449 break;
2450
2451 case ISN_STOREOPT:
2452 case ISN_STOREFUNCOPT:
2453 vim_free(isn->isn_arg.storeopt.so_name);
2454 break;
2455
2456 case ISN_PUSHBLOB: // push blob isn_arg.blob
2457 blob_unref(isn->isn_arg.blob);
2458 break;
2459
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002460 case ISN_UCALL:
2461 vim_free(isn->isn_arg.ufunc.cuf_name);
2462 break;
2463
2464 case ISN_FUNCREF:
2465 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002466 funcref_T *funcref = &isn->isn_arg.funcref;
2467 funcref_extra_T *extra = funcref->fr_extra;
2468
2469 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002470 {
2471 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002472 + funcref->fr_dfunc_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002473 ufunc_T *ufunc = dfunc->df_ufunc;
2474
2475 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2476 func_ptr_unref(ufunc);
2477 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002478 if (extra != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002479 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002480 char_u *name = extra->fre_func_name;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002481
2482 if (name != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002483 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002484 func_unref(name);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002485 vim_free(name);
2486 }
2487 vim_free(extra);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002488 }
2489 }
2490 break;
2491
2492 case ISN_DCALL:
2493 {
2494 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002495 + isn->isn_arg.dfunc.cdf_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002496
2497 if (dfunc->df_ufunc != NULL
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002498 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002499 func_ptr_unref(dfunc->df_ufunc);
2500 }
2501 break;
2502
Bram Moolenaard0200c82023-01-28 15:19:40 +00002503 case ISN_METHODCALL:
2504 {
2505 cmfunc_T *mfunc = isn->isn_arg.mfunc;
2506 class_unref(mfunc->cmf_itf);
2507 vim_free(mfunc);
2508 }
2509 break;
2510
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002511 case ISN_NEWFUNC:
2512 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002513 newfuncarg_T *arg = isn->isn_arg.newfunc.nf_arg;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002514
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002515 if (arg != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002516 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002517 ufunc_T *ufunc = find_func_even_dead(
2518 arg->nfa_lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002519
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002520 if (ufunc != NULL)
2521 {
2522 unlink_def_function(ufunc);
2523 func_ptr_unref(ufunc);
2524 }
2525
2526 vim_free(arg->nfa_lambda);
2527 vim_free(arg->nfa_global);
2528 vim_free(arg);
2529 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002530 }
2531 break;
2532
2533 case ISN_CHECKTYPE:
2534 case ISN_SETTYPE:
2535 free_type(isn->isn_arg.type.ct_type);
2536 break;
2537
2538 case ISN_CMDMOD:
2539 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002540 ->cmod_filter_regmatch.regprog);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002541 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2542 break;
2543
2544 case ISN_LOADSCRIPT:
2545 case ISN_STORESCRIPT:
2546 vim_free(isn->isn_arg.script.scriptref);
2547 break;
2548
Bram Moolenaard505d172022-12-18 21:42:55 +00002549 case ISN_LOAD_CLASSMEMBER:
2550 case ISN_STORE_CLASSMEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00002551 case ISN_GET_ITF_MEMBER:
Bram Moolenaard505d172022-12-18 21:42:55 +00002552 class_unref(isn->isn_arg.classmember.cm_class);
2553 break;
2554
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002555 case ISN_STOREINDEX:
2556 class_unref(isn->isn_arg.storeindex.si_class);
2557 break;
2558
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002559 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002560 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002561 break;
2562
2563 case ISN_CEXPR_CORE:
2564 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2565 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2566 break;
2567
2568 case ISN_2BOOL:
2569 case ISN_2STRING:
2570 case ISN_2STRING_ANY:
2571 case ISN_ADDBLOB:
2572 case ISN_ADDLIST:
2573 case ISN_ANYINDEX:
2574 case ISN_ANYSLICE:
2575 case ISN_BCALL:
2576 case ISN_BLOBAPPEND:
2577 case ISN_BLOBINDEX:
2578 case ISN_BLOBSLICE:
2579 case ISN_CATCH:
2580 case ISN_CEXPR_AUCMD:
2581 case ISN_CHECKLEN:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002582 case ISN_CLEARDICT:
2583 case ISN_CMDMOD_REV:
2584 case ISN_COMPAREANY:
2585 case ISN_COMPAREBLOB:
2586 case ISN_COMPAREBOOL:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002587 case ISN_COMPARECLASS:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002588 case ISN_COMPAREDICT:
2589 case ISN_COMPAREFLOAT:
2590 case ISN_COMPAREFUNC:
2591 case ISN_COMPARELIST:
2592 case ISN_COMPARENR:
Bram Moolenaar7a222242022-03-01 19:23:24 +00002593 case ISN_COMPARENULL:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002594 case ISN_COMPAREOBJECT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002595 case ISN_COMPARESPECIAL:
2596 case ISN_COMPARESTRING:
2597 case ISN_CONCAT:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002598 case ISN_CONSTRUCT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002599 case ISN_COND2BOOL:
2600 case ISN_DEBUG:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002601 case ISN_DEFER:
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00002602 case ISN_DEFEROBJ:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002603 case ISN_DROP:
2604 case ISN_ECHO:
2605 case ISN_ECHOCONSOLE:
2606 case ISN_ECHOERR:
2607 case ISN_ECHOMSG:
Bram Moolenaar68a635a2022-09-01 17:26:17 +01002608 case ISN_ECHOWINDOW:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002609 case ISN_ENDLOOP:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002610 case ISN_ENDTRY:
2611 case ISN_EXECCONCAT:
2612 case ISN_EXECUTE:
2613 case ISN_FINALLY:
2614 case ISN_FINISH:
2615 case ISN_FOR:
2616 case ISN_GETITEM:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002617 case ISN_GET_OBJ_MEMBER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002618 case ISN_JUMP:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002619 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002620 case ISN_JUMP_IF_ARG_SET:
2621 case ISN_LISTAPPEND:
2622 case ISN_LISTINDEX:
2623 case ISN_LISTSLICE:
2624 case ISN_LOAD:
2625 case ISN_LOADBDICT:
2626 case ISN_LOADGDICT:
2627 case ISN_LOADOUTER:
2628 case ISN_LOADREG:
2629 case ISN_LOADTDICT:
2630 case ISN_LOADV:
2631 case ISN_LOADWDICT:
2632 case ISN_LOCKCONST:
2633 case ISN_MEMBER:
2634 case ISN_NEGATENR:
2635 case ISN_NEWDICT:
2636 case ISN_NEWLIST:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002637 case ISN_NEWPARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002638 case ISN_OPANY:
2639 case ISN_OPFLOAT:
2640 case ISN_OPNR:
2641 case ISN_PCALL:
2642 case ISN_PCALL_END:
2643 case ISN_PROF_END:
2644 case ISN_PROF_START:
2645 case ISN_PUSHBOOL:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002646 case ISN_PUSHCHANNEL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002647 case ISN_PUSHF:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002648 case ISN_PUSHJOB:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002649 case ISN_PUSHNR:
2650 case ISN_PUSHSPEC:
2651 case ISN_PUT:
2652 case ISN_REDIREND:
2653 case ISN_REDIRSTART:
2654 case ISN_RETURN:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002655 case ISN_RETURN_OBJECT:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002656 case ISN_RETURN_VOID:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002657 case ISN_SHUFFLE:
2658 case ISN_SLICE:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002659 case ISN_SOURCE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002660 case ISN_STORE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002661 case ISN_STORENR:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002662 case ISN_STOREOUTER:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002663 case ISN_STORE_THIS:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002664 case ISN_STORERANGE:
2665 case ISN_STOREREG:
2666 case ISN_STOREV:
2667 case ISN_STRINDEX:
2668 case ISN_STRSLICE:
2669 case ISN_THROW:
2670 case ISN_TRYCONT:
2671 case ISN_UNLETINDEX:
2672 case ISN_UNLETRANGE:
2673 case ISN_UNPACK:
2674 case ISN_USEDICT:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002675 case ISN_WHILE:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002676 // nothing allocated
2677 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002678 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002679}
2680
2681 void
2682clear_instr_ga(garray_T *gap)
2683{
2684 int idx;
2685
2686 for (idx = 0; idx < gap->ga_len; ++idx)
2687 delete_instr(((isn_T *)gap->ga_data) + idx);
2688 ga_clear(gap);
2689}
2690
2691
2692#endif // defined(FEAT_EVAL)