blob: cdf037942c71a599796ba1cd8c8df919ac5b4ca2 [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 Moolenaar313e4722023-02-08 20:55:27 +00001331 * For "obj.Method" "cl" is the class of the object (can be an interface or a
1332 * base class) and "fi" the index of the method on that class.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001333 * "isnp" is set to the instruction, so that fr_dfunc_idx can be set later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001334 */
1335 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001336generate_FUNCREF(
1337 cctx_T *cctx,
1338 ufunc_T *ufunc,
Bram Moolenaar313e4722023-02-08 20:55:27 +00001339 class_T *cl,
1340 int fi,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001341 isn_T **isnp)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001342{
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001343 isn_T *isn;
1344 type_T *type;
1345 funcref_extra_T *extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001346 loopvarinfo_T loopinfo;
1347 int has_vars;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001348
1349 RETURN_OK_IF_SKIP(cctx);
1350 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1351 return FAIL;
Bram Moolenaara915fa02022-03-23 11:29:15 +00001352 if (isnp != NULL)
1353 *isnp = isn;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001354
Bram Moolenaarcc341812022-09-19 15:54:34 +01001355 has_vars = get_loop_var_info(cctx, &loopinfo);
Bram Moolenaar313e4722023-02-08 20:55:27 +00001356 if (ufunc->uf_def_status == UF_NOT_COMPILED || has_vars || cl != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001357 {
1358 extra = ALLOC_CLEAR_ONE(funcref_extra_T);
1359 if (extra == NULL)
1360 return FAIL;
1361 isn->isn_arg.funcref.fr_extra = extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001362 extra->fre_loopvar_info = loopinfo;
Bram Moolenaar313e4722023-02-08 20:55:27 +00001363 if (cl != NULL)
1364 {
1365 extra->fre_class = cl;
1366 ++cl->class_refcount;
1367 extra->fre_method_idx = fi;
1368 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001369 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00001370 if (ufunc->uf_def_status == UF_NOT_COMPILED || cl != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001371 extra->fre_func_name = vim_strsave(ufunc->uf_name);
Bram Moolenaar313e4722023-02-08 20:55:27 +00001372 if (ufunc->uf_def_status != UF_NOT_COMPILED && cl == NULL)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001373 {
1374 if (isnp == NULL && ufunc->uf_def_status == UF_TO_BE_COMPILED)
1375 // compile the function now, we need the uf_dfunc_idx value
1376 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001377 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001378 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01001379
1380 // Reserve an extra variable to keep track of the number of closures
1381 // created.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001382 cctx->ctx_has_closure = 1;
1383
1384 // If the referenced function is a closure, it may use items further up in
Bram Moolenaar139575d2022-03-15 19:29:30 +00001385 // the nested context, including this one. But not a function defined at
1386 // the script level.
1387 if ((ufunc->uf_flags & FC_CLOSURE)
1388 && func_name_refcount(cctx->ctx_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001389 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1390
Bram Moolenaar078a4612022-01-04 15:17:03 +00001391 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1392 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001393}
1394
1395/*
1396 * Generate an ISN_NEWFUNC instruction.
1397 * "lambda_name" and "func_name" must be in allocated memory and will be
1398 * consumed.
1399 */
1400 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001401generate_NEWFUNC(
1402 cctx_T *cctx,
1403 char_u *lambda_name,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001404 char_u *func_name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001405{
1406 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001407 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001408
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001409 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001410 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001411 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1412 ret = FAIL;
1413 else
1414 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001415 newfuncarg_T *arg = ALLOC_CLEAR_ONE(newfuncarg_T);
1416
1417 if (arg == NULL)
1418 ret = FAIL;
1419 else
1420 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01001421 // Reserve an extra variable to keep track of the number of
1422 // closures created.
1423 cctx->ctx_has_closure = 1;
1424
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001425 isn->isn_arg.newfunc.nf_arg = arg;
1426 arg->nfa_lambda = lambda_name;
1427 arg->nfa_global = func_name;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001428 (void)get_loop_var_info(cctx, &arg->nfa_loopvar_info);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001429 return OK;
1430 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001431 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001432 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001433 vim_free(lambda_name);
1434 vim_free(func_name);
1435 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001436}
1437
1438/*
1439 * Generate an ISN_DEF instruction: list functions
1440 */
1441 int
1442generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1443{
1444 isn_T *isn;
1445
1446 RETURN_OK_IF_SKIP(cctx);
1447 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1448 return FAIL;
1449 if (len > 0)
1450 {
1451 isn->isn_arg.string = vim_strnsave(name, len);
1452 if (isn->isn_arg.string == NULL)
1453 return FAIL;
1454 }
1455 return OK;
1456}
1457
1458/*
1459 * Generate an ISN_JUMP instruction.
1460 */
1461 int
1462generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1463{
1464 isn_T *isn;
1465 garray_T *stack = &cctx->ctx_type_stack;
1466
1467 RETURN_OK_IF_SKIP(cctx);
1468 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1469 return FAIL;
1470 isn->isn_arg.jump.jump_when = when;
1471 isn->isn_arg.jump.jump_where = where;
1472
1473 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1474 --stack->ga_len;
1475
1476 return OK;
1477}
1478
1479/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001480 * Generate an ISN_WHILE instruction. Similar to ISN_JUMP for :while
1481 */
1482 int
1483generate_WHILE(cctx_T *cctx, int funcref_idx)
1484{
1485 isn_T *isn;
1486 garray_T *stack = &cctx->ctx_type_stack;
1487
1488 RETURN_OK_IF_SKIP(cctx);
1489 if ((isn = generate_instr(cctx, ISN_WHILE)) == NULL)
1490 return FAIL;
1491 isn->isn_arg.whileloop.while_funcref_idx = funcref_idx;
1492 isn->isn_arg.whileloop.while_end = 0; // filled in later
1493
1494 if (stack->ga_len > 0)
1495 --stack->ga_len;
1496
1497 return OK;
1498}
1499
1500/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001501 * Generate an ISN_JUMP_IF_ARG_SET or ISN_JUMP_IF_ARG_NOT_SET instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001502 */
1503 int
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001504generate_JUMP_IF_ARG(cctx_T *cctx, isntype_T isn_type, int arg_off)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001505{
1506 isn_T *isn;
1507
1508 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001509 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001510 return FAIL;
1511 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1512 // jump_where is set later
1513 return OK;
1514}
1515
1516 int
1517generate_FOR(cctx_T *cctx, int loop_idx)
1518{
1519 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001520
1521 RETURN_OK_IF_SKIP(cctx);
1522 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1523 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001524 isn->isn_arg.forloop.for_loop_idx = loop_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001525
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001526 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001527 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001528}
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001529
1530 int
Bram Moolenaarcc341812022-09-19 15:54:34 +01001531generate_ENDLOOP(cctx_T *cctx, loop_info_T *loop_info)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001532{
1533 isn_T *isn;
1534
1535 RETURN_OK_IF_SKIP(cctx);
1536 if ((isn = generate_instr(cctx, ISN_ENDLOOP)) == NULL)
1537 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001538 isn->isn_arg.endloop.end_depth = loop_info->li_depth;
1539 isn->isn_arg.endloop.end_funcref_idx = loop_info->li_funcref_idx;
1540 isn->isn_arg.endloop.end_var_idx = loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001541 isn->isn_arg.endloop.end_var_count =
Bram Moolenaarcc341812022-09-19 15:54:34 +01001542 cctx->ctx_locals.ga_len - loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001543 return OK;
1544}
1545
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001546/*
1547 * Generate an ISN_TRYCONT instruction.
1548 */
1549 int
1550generate_TRYCONT(cctx_T *cctx, int levels, int where)
1551{
1552 isn_T *isn;
1553
1554 RETURN_OK_IF_SKIP(cctx);
1555 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1556 return FAIL;
1557 isn->isn_arg.trycont.tct_levels = levels;
1558 isn->isn_arg.trycont.tct_where = where;
1559
1560 return OK;
1561}
1562
Bram Moolenaar16900322022-09-08 19:51:45 +01001563/*
1564 * Check "argount" arguments and their types on the type stack.
1565 * Give an error and return FAIL if something is wrong.
1566 * When "method_call" is NULL no code is generated.
1567 */
1568 int
1569check_internal_func_args(
1570 cctx_T *cctx,
1571 int func_idx,
1572 int argcount,
1573 int method_call,
1574 type2_T **argtypes,
1575 type2_T *shuffled_argtypes)
1576{
1577 garray_T *stack = &cctx->ctx_type_stack;
1578 int argoff = check_internal_func(func_idx, argcount);
1579
1580 if (argoff < 0)
1581 return FAIL;
1582
1583 if (method_call && argoff > 1)
1584 {
1585 isn_T *isn = generate_instr(cctx, ISN_SHUFFLE);
1586
1587 if (isn == NULL)
1588 return FAIL;
1589 isn->isn_arg.shuffle.shfl_item = argcount;
1590 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1591 }
1592
1593 if (argcount > 0)
1594 {
1595 type2_T *typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
1596
1597 // Check the types of the arguments.
1598 if (method_call && argoff > 1)
1599 {
1600 int i;
1601
1602 for (i = 0; i < argcount; ++i)
1603 shuffled_argtypes[i] = (i < argoff - 1)
1604 ? typep[i + 1]
1605 : (i == argoff - 1) ? typep[0] : typep[i];
1606 *argtypes = shuffled_argtypes;
1607 }
1608 else
1609 {
1610 int i;
1611
1612 for (i = 0; i < argcount; ++i)
1613 shuffled_argtypes[i] = typep[i];
1614 *argtypes = shuffled_argtypes;
1615 }
1616 if (internal_func_check_arg_types(*argtypes, func_idx, argcount,
1617 cctx) == FAIL)
1618 return FAIL;
1619 }
1620 return OK;
1621}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001622
1623/*
1624 * Generate an ISN_BCALL instruction.
1625 * "method_call" is TRUE for "value->method()"
1626 * Return FAIL if the number of arguments is wrong.
1627 */
1628 int
1629generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1630{
1631 isn_T *isn;
1632 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001633 type2_T *argtypes = NULL;
1634 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1635 type2_T *maptype = NULL;
1636 type_T *type;
Bram Moolenaar81330182022-02-01 12:11:58 +00001637 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001638
1639 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar16900322022-09-08 19:51:45 +01001640
1641 if (check_internal_func_args(cctx, func_idx, argcount, method_call,
1642 &argtypes, shuffled_argtypes) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001643 return FAIL;
1644
Bram Moolenaar16900322022-09-08 19:51:45 +01001645 if (internal_func_is_map(func_idx))
1646 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001647
1648 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1649 return FAIL;
1650 isn->isn_arg.bfunc.cbf_idx = func_idx;
1651 isn->isn_arg.bfunc.cbf_argcount = argcount;
1652
1653 // Drop the argument types and push the return type.
1654 stack->ga_len -= argcount;
Bram Moolenaar32517c42023-01-15 18:17:12 +00001655 type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type,
1656 cctx->ctx_type_list);
Bram Moolenaar81330182022-02-01 12:11:58 +00001657 if (push_type_stack2(cctx, type, decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001658 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001659
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001660 if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
1661 && maptype[0].type_decl->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001662 // Check that map() didn't change the item types.
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001663 generate_TYPECHECK(cctx, maptype[0].type_decl, FALSE, -1, FALSE, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001664
1665 return OK;
1666}
1667
1668/*
1669 * Generate an ISN_LISTAPPEND instruction. Works like add().
1670 * Argument count is already checked.
1671 */
1672 int
1673generate_LISTAPPEND(cctx_T *cctx)
1674{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001675 type_T *list_type;
1676 type_T *item_type;
1677 type_T *expected;
1678
1679 // Caller already checked that list_type is a list.
Bram Moolenaar81330182022-02-01 12:11:58 +00001680 // For checking the item type we use the declared type of the list and the
1681 // current type of the added item, adding a string to [1, 2] is OK.
1682 list_type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaarfa103972022-09-29 19:14:42 +01001683 if (arg_type_modifiable(list_type, 1) == FAIL)
1684 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001685 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001686 expected = list_type->tt_member;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001687 if (need_type(item_type, expected, FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001688 return FAIL;
1689
1690 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1691 return FAIL;
1692
Bram Moolenaar078a4612022-01-04 15:17:03 +00001693 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001694 return OK;
1695}
1696
1697/*
1698 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1699 * Argument count is already checked.
1700 */
1701 int
1702generate_BLOBAPPEND(cctx_T *cctx)
1703{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001704 type_T *item_type;
1705
Bram Moolenaarfa103972022-09-29 19:14:42 +01001706 // Caller already checked that blob_type is a blob, check it is modifiable.
1707 if (arg_type_modifiable(get_decl_type_on_stack(cctx, 1), 1) == FAIL)
1708 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001709 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001710 if (need_type(item_type, &t_number, FALSE,
1711 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001712 return FAIL;
1713
1714 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1715 return FAIL;
1716
Bram Moolenaar078a4612022-01-04 15:17:03 +00001717 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001718 return OK;
1719}
1720
1721/*
Bram Moolenaard0200c82023-01-28 15:19:40 +00001722 * Generate an ISN_DCALL, ISN_UCALL or ISN_METHODCALL instruction.
1723 * When calling a method on an object, of which we know the interface only,
1724 * then "cl" is the interface and "mi" the method index on the interface.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001725 * Return FAIL if the number of arguments is wrong.
1726 */
1727 int
Bram Moolenaard0200c82023-01-28 15:19:40 +00001728generate_CALL(
1729 cctx_T *cctx,
1730 ufunc_T *ufunc,
1731 class_T *cl,
1732 int mi,
1733 int pushed_argcount)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001734{
1735 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001736 int regular_args = ufunc->uf_args.ga_len;
1737 int argcount = pushed_argcount;
1738
1739 RETURN_OK_IF_SKIP(cctx);
1740 if (argcount > regular_args && !has_varargs(ufunc))
1741 {
1742 semsg(_(e_too_many_arguments_for_function_str),
1743 printable_func_name(ufunc));
1744 return FAIL;
1745 }
1746 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1747 {
1748 semsg(_(e_not_enough_arguments_for_function_str),
1749 printable_func_name(ufunc));
1750 return FAIL;
1751 }
1752
1753 if (ufunc->uf_def_status != UF_NOT_COMPILED
1754 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1755 {
1756 int i;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001757 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001758
1759 for (i = 0; i < argcount; ++i)
1760 {
1761 type_T *expected;
1762 type_T *actual;
1763
Bram Moolenaar078a4612022-01-04 15:17:03 +00001764 actual = get_type_on_stack(cctx, argcount - i - 1);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001765 if (actual->tt_type == VAR_SPECIAL
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001766 && i >= regular_args - ufunc->uf_def_args.ga_len)
1767 {
1768 // assume v:none used for default argument value
1769 continue;
1770 }
1771 if (i < regular_args)
1772 {
1773 if (ufunc->uf_arg_types == NULL)
1774 continue;
1775 expected = ufunc->uf_arg_types[i];
1776 }
1777 else if (ufunc->uf_va_type == NULL
1778 || ufunc->uf_va_type == &t_list_any)
1779 // possibly a lambda or "...: any"
1780 expected = &t_any;
1781 else
1782 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001783 if (need_type(actual, expected, FALSE,
1784 -argcount + i, i + 1, cctx, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001785 {
1786 arg_type_mismatch(expected, actual, i + 1);
1787 return FAIL;
1788 }
1789 }
Bram Moolenaar139575d2022-03-15 19:29:30 +00001790 compile_type = get_compile_type(ufunc);
1791 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001792 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaar139575d2022-03-15 19:29:30 +00001793 compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001794 return FAIL;
1795 }
1796 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1797 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001798 emsg_funcname(e_call_to_function_that_failed_to_compile_str,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001799 ufunc->uf_name);
1800 return FAIL;
1801 }
1802
Bram Moolenaard0200c82023-01-28 15:19:40 +00001803 if ((isn = generate_instr(cctx, cl != NULL ? ISN_METHODCALL
1804 : ufunc->uf_def_status != UF_NOT_COMPILED
1805 ? ISN_DCALL : ISN_UCALL)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001806 return FAIL;
Bram Moolenaar836137d2023-01-29 14:11:24 +00001807 if (cl != NULL /* isn->isn_type == ISN_METHODCALL */)
Bram Moolenaard0200c82023-01-28 15:19:40 +00001808 {
1809 isn->isn_arg.mfunc = ALLOC_ONE(cmfunc_T);
1810 if (isn->isn_arg.mfunc == NULL)
1811 return FAIL;
1812 isn->isn_arg.mfunc->cmf_itf = cl;
1813 ++cl->class_refcount;
1814 isn->isn_arg.mfunc->cmf_idx = mi;
1815 isn->isn_arg.mfunc->cmf_argcount = argcount;
1816 }
1817 else if (isn->isn_type == ISN_DCALL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001818 {
1819 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1820 isn->isn_arg.dfunc.cdf_argcount = argcount;
1821 }
1822 else
1823 {
1824 // A user function may be deleted and redefined later, can't use the
1825 // ufunc pointer, need to look it up again at runtime.
1826 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1827 isn->isn_arg.ufunc.cuf_argcount = argcount;
1828 }
1829
Bram Moolenaar078a4612022-01-04 15:17:03 +00001830 // drop the argument types
1831 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001832
Bram Moolenaar078a4612022-01-04 15:17:03 +00001833 // add return type
1834 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001835}
1836
1837/*
1838 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1839 */
1840 int
1841generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1842{
1843 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001844
1845 RETURN_OK_IF_SKIP(cctx);
1846 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1847 return FAIL;
1848 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1849 isn->isn_arg.ufunc.cuf_argcount = argcount;
1850
Bram Moolenaar078a4612022-01-04 15:17:03 +00001851 // drop the argument types
1852 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001853
Bram Moolenaar078a4612022-01-04 15:17:03 +00001854 // add return value
1855 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001856}
1857
1858/*
Bram Moolenaar16900322022-09-08 19:51:45 +01001859 * Check the arguments of function "type" against the types on the stack.
1860 * Returns OK or FAIL;
1861 */
1862 int
1863check_func_args_from_type(
1864 cctx_T *cctx,
1865 type_T *type,
1866 int argcount,
1867 int at_top,
1868 char_u *name)
1869{
1870 if (type->tt_argcount != -1)
1871 {
1872 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1873
1874 if (argcount < type->tt_min_argcount - varargs)
1875 {
1876 emsg_funcname(e_not_enough_arguments_for_function_str, name);
1877 return FAIL;
1878 }
1879 if (!varargs && argcount > type->tt_argcount)
1880 {
1881 emsg_funcname(e_too_many_arguments_for_function_str, name);
1882 return FAIL;
1883 }
1884 if (type->tt_args != NULL)
1885 {
1886 int i;
1887
1888 for (i = 0; i < argcount; ++i)
1889 {
1890 int offset = -argcount + i - (at_top ? 0 : 1);
1891 type_T *actual = get_type_on_stack(cctx, -1 - offset);
1892 type_T *expected;
1893
1894 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaar36818a92023-01-03 12:33:26 +00001895 {
1896 expected = type->tt_args[type->tt_argcount - 1];
1897 if (expected != NULL && expected->tt_type == VAR_LIST)
1898 expected = expected->tt_member;
1899 if (expected == NULL)
1900 expected = &t_any;
1901 }
Bram Moolenaar16900322022-09-08 19:51:45 +01001902 else if (i >= type->tt_min_argcount
1903 && actual->tt_type == VAR_SPECIAL)
1904 expected = &t_any;
1905 else
1906 expected = type->tt_args[i];
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001907 if (need_type(actual, expected, FALSE,
1908 offset, i + 1, cctx, TRUE, FALSE) == FAIL)
Bram Moolenaar16900322022-09-08 19:51:45 +01001909 {
1910 arg_type_mismatch(expected, actual, i + 1);
1911 return FAIL;
1912 }
1913 }
1914 }
1915 }
1916
1917 return OK;
1918}
1919/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001920 * Generate an ISN_PCALL instruction.
1921 * "type" is the type of the FuncRef.
1922 */
1923 int
1924generate_PCALL(
1925 cctx_T *cctx,
1926 int argcount,
1927 char_u *name,
1928 type_T *type,
1929 int at_top)
1930{
1931 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001932 type_T *ret_type;
1933
1934 RETURN_OK_IF_SKIP(cctx);
1935
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001936 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001937 ret_type = &t_any;
1938 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1939 {
Bram Moolenaar16900322022-09-08 19:51:45 +01001940 if (check_func_args_from_type(cctx, type, argcount, at_top, name) == FAIL)
1941 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001942
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001943 ret_type = type->tt_member;
1944 if (ret_type == &t_unknown)
1945 // return type not known yet, use a runtime check
1946 ret_type = &t_any;
1947 }
1948 else
1949 {
1950 semsg(_(e_not_callable_type_str), name);
1951 return FAIL;
1952 }
1953
1954 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1955 return FAIL;
1956 isn->isn_arg.pfunc.cpf_top = at_top;
1957 isn->isn_arg.pfunc.cpf_argcount = argcount;
1958
Bram Moolenaar078a4612022-01-04 15:17:03 +00001959 // drop the arguments and the funcref/partial
1960 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001961
Bram Moolenaar078a4612022-01-04 15:17:03 +00001962 // push the return value
1963 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001964
1965 // If partial is above the arguments it must be cleared and replaced with
1966 // the return value.
1967 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1968 return FAIL;
1969
1970 return OK;
1971}
1972
1973/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001974 * Generate an ISN_DEFER instruction.
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001975 * "obj_method" is one for "obj.Method()", zero otherwise.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001976 */
1977 int
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001978generate_DEFER(cctx_T *cctx, int var_idx, int obj_method, int argcount)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001979{
1980 isn_T *isn;
1981
1982 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001983 if ((isn = generate_instr_drop(cctx,
1984 obj_method == 0 ? ISN_DEFER : ISN_DEFEROBJ,
1985 argcount + 1)) == NULL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001986 return FAIL;
1987 isn->isn_arg.defer.defer_var_idx = var_idx;
1988 isn->isn_arg.defer.defer_argcount = argcount;
1989 return OK;
1990}
1991
1992/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001993 * Generate an ISN_STRINGMEMBER instruction.
1994 */
1995 int
1996generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
1997{
1998 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001999 type_T *type;
2000
2001 RETURN_OK_IF_SKIP(cctx);
2002 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
2003 return FAIL;
2004 isn->isn_arg.string = vim_strnsave(name, len);
2005
2006 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00002007 type = get_type_on_stack(cctx, 0);
Bram Moolenaar0089ce22022-10-08 14:39:36 +01002008 if (type->tt_type != VAR_DICT
2009 && type->tt_type != VAR_ANY && type->tt_type != VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002010 {
2011 char *tofree;
2012
2013 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
2014 name, type_name(type, &tofree));
2015 vim_free(tofree);
2016 return FAIL;
2017 }
2018 // change dict type to dict member type
2019 if (type->tt_type == VAR_DICT)
2020 {
Bram Moolenaar0089ce22022-10-08 14:39:36 +01002021 type_T *ntype = type->tt_member->tt_type == VAR_UNKNOWN
Bram Moolenaar078a4612022-01-04 15:17:03 +00002022 ? &t_any : type->tt_member;
2023 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002024 }
2025
2026 return OK;
2027}
2028
2029/*
2030 * Generate an ISN_ECHO instruction.
2031 */
2032 int
2033generate_ECHO(cctx_T *cctx, int with_white, int count)
2034{
2035 isn_T *isn;
2036
2037 RETURN_OK_IF_SKIP(cctx);
2038 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2039 return FAIL;
2040 isn->isn_arg.echo.echo_with_white = with_white;
2041 isn->isn_arg.echo.echo_count = count;
2042
2043 return OK;
2044}
2045
2046/*
2047 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
2048 */
2049 int
2050generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
2051{
2052 isn_T *isn;
2053
Bram Moolenaar2eae3d22022-10-07 15:09:27 +01002054 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002055 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
2056 return FAIL;
2057 isn->isn_arg.number = count;
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002058 return OK;
2059}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002060
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002061/*
2062 * Generate an ISN_ECHOWINDOW instruction
2063 */
2064 int
2065generate_ECHOWINDOW(cctx_T *cctx, int count, long time)
2066{
2067 isn_T *isn;
2068
2069 if ((isn = generate_instr_drop(cctx, ISN_ECHOWINDOW, count)) == NULL)
2070 return FAIL;
2071 isn->isn_arg.echowin.ewin_count = count;
2072 isn->isn_arg.echowin.ewin_time = time;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002073 return OK;
2074}
2075
2076/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002077 * Generate an ISN_SOURCE instruction.
2078 */
2079 int
2080generate_SOURCE(cctx_T *cctx, int sid)
2081{
2082 isn_T *isn;
2083
2084 if ((isn = generate_instr(cctx, ISN_SOURCE)) == NULL)
2085 return FAIL;
2086 isn->isn_arg.number = sid;
2087
2088 return OK;
2089}
2090
2091/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002092 * Generate an ISN_PUT instruction.
2093 */
2094 int
2095generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2096{
2097 isn_T *isn;
2098
2099 RETURN_OK_IF_SKIP(cctx);
2100 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2101 return FAIL;
2102 isn->isn_arg.put.put_regname = regname;
2103 isn->isn_arg.put.put_lnum = lnum;
2104 return OK;
2105}
2106
2107/*
2108 * Generate an EXEC instruction that takes a string argument.
2109 * A copy is made of "line".
2110 */
2111 int
2112generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
2113{
2114 isn_T *isn;
2115
2116 RETURN_OK_IF_SKIP(cctx);
2117 if ((isn = generate_instr(cctx, isntype)) == NULL)
2118 return FAIL;
2119 isn->isn_arg.string = vim_strsave(line);
2120 return OK;
2121}
2122
2123/*
2124 * Generate an EXEC instruction that takes a string argument.
2125 * "str" must be allocated, it is consumed.
2126 */
2127 int
2128generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
2129{
2130 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002131 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002132
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002133 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002134 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002135 if ((isn = generate_instr(cctx, isntype)) == NULL)
2136 ret = FAIL;
2137 else
2138 {
2139 isn->isn_arg.string = str;
2140 return OK;
2141 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002142 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002143 vim_free(str);
2144 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002145}
2146
2147 int
2148generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2149{
2150 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002151
2152 RETURN_OK_IF_SKIP(cctx);
2153 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2154 return FAIL;
2155 isn->isn_arg.string = vim_strsave(line);
2156
Bram Moolenaar078a4612022-01-04 15:17:03 +00002157 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002158}
2159
2160 int
2161generate_EXECCONCAT(cctx_T *cctx, int count)
2162{
2163 isn_T *isn;
2164
2165 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2166 return FAIL;
2167 isn->isn_arg.number = count;
2168 return OK;
2169}
2170
2171/*
2172 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2173 */
2174 int
2175generate_RANGE(cctx_T *cctx, char_u *range)
2176{
2177 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002178
2179 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2180 return FAIL;
2181 isn->isn_arg.string = range;
2182
Bram Moolenaar078a4612022-01-04 15:17:03 +00002183 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002184}
2185
2186 int
2187generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2188{
2189 isn_T *isn;
2190
2191 RETURN_OK_IF_SKIP(cctx);
2192 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2193 return FAIL;
2194 isn->isn_arg.unpack.unp_count = var_count;
2195 isn->isn_arg.unpack.unp_semicolon = semicolon;
2196 return OK;
2197}
2198
2199/*
2200 * Generate an instruction for any command modifiers.
2201 */
2202 int
2203generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
2204{
2205 isn_T *isn;
2206
2207 if (has_cmdmod(cmod, FALSE))
2208 {
2209 cctx->ctx_has_cmdmod = TRUE;
2210
2211 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
2212 return FAIL;
2213 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2214 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2215 return FAIL;
2216 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
2217 // filter program now belongs to the instruction
2218 cmod->cmod_filter_regmatch.regprog = NULL;
2219 }
2220
2221 return OK;
2222}
2223
2224 int
2225generate_undo_cmdmods(cctx_T *cctx)
2226{
2227 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2228 return FAIL;
2229 cctx->ctx_has_cmdmod = FALSE;
2230 return OK;
2231}
2232
2233/*
2234 * Generate a STORE instruction for "dest", not being "dest_local".
Bram Moolenaard505d172022-12-18 21:42:55 +00002235 * "lhs" might be NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002236 * Return FAIL when out of memory.
2237 */
2238 int
2239generate_store_var(
2240 cctx_T *cctx,
2241 assign_dest_T dest,
2242 int opt_flags,
2243 int vimvaridx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002244 type_T *type,
Bram Moolenaard505d172022-12-18 21:42:55 +00002245 char_u *name,
2246 lhs_T *lhs)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002247{
2248 switch (dest)
2249 {
2250 case dest_option:
2251 return generate_STOREOPT(cctx, ISN_STOREOPT,
2252 skip_option_env_lead(name), opt_flags);
2253 case dest_func_option:
2254 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
2255 skip_option_env_lead(name), opt_flags);
2256 case dest_global:
2257 // include g: with the name, easier to execute that way
2258 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
2259 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
2260 case dest_buffer:
2261 // include b: with the name, easier to execute that way
2262 return generate_STORE(cctx, ISN_STOREB, 0, name);
2263 case dest_window:
2264 // include w: with the name, easier to execute that way
2265 return generate_STORE(cctx, ISN_STOREW, 0, name);
2266 case dest_tab:
2267 // include t: with the name, easier to execute that way
2268 return generate_STORE(cctx, ISN_STORET, 0, name);
2269 case dest_env:
2270 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
2271 case dest_reg:
2272 return generate_STORE(cctx, ISN_STOREREG,
2273 name[1] == '@' ? '"' : name[1], NULL);
2274 case dest_vimvar:
2275 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
2276 case dest_script:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002277 {
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002278 int scriptvar_idx = lhs->lhs_scriptvar_idx;
2279 int scriptvar_sid = lhs->lhs_scriptvar_sid;
2280 if (scriptvar_idx < 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002281 {
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002282 isntype_T isn_type = ISN_STORES;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002283
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002284 if (SCRIPT_ID_VALID(scriptvar_sid)
2285 && SCRIPT_ITEM(scriptvar_sid)->sn_import_autoload
2286 && SCRIPT_ITEM(scriptvar_sid)->sn_autoload_prefix
2287 == NULL)
2288 {
2289 // "import autoload './dir/script.vim'" - load script
2290 // first
2291 if (generate_SOURCE(cctx, scriptvar_sid) == FAIL)
2292 return FAIL;
2293 isn_type = ISN_STOREEXPORT;
2294 }
2295
2296 // "s:" may be included in the name.
2297 return generate_OLDSCRIPT(cctx, isn_type, name,
2298 scriptvar_sid, type);
2299 }
2300 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002301 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002302 }
Bram Moolenaard505d172022-12-18 21:42:55 +00002303 case dest_class_member:
2304 return generate_CLASSMEMBER(cctx, FALSE,
2305 lhs->lhs_class, lhs->lhs_classmember_idx);
2306
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002307 case dest_local:
2308 case dest_expr:
2309 // cannot happen
2310 break;
2311 }
2312 return FAIL;
2313}
2314
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002315/*
2316 * Return TRUE when inside a "for" or "while" loop.
2317 */
2318 int
2319inside_loop_scope(cctx_T *cctx)
2320{
2321 scope_T *scope = cctx->ctx_scope;
2322
2323 for (;;)
2324 {
2325 if (scope == NULL)
2326 break;
2327 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
2328 return TRUE;
2329 scope = scope->se_outer;
2330 }
2331 return FALSE;
2332}
2333
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002334 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002335generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002336{
2337 if (lhs->lhs_dest != dest_local)
2338 return generate_store_var(cctx, lhs->lhs_dest,
2339 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
Bram Moolenaard505d172022-12-18 21:42:55 +00002340 lhs->lhs_type, lhs->lhs_name, lhs);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002341
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002342 if (lhs->lhs_lvar == NULL)
2343 return OK;
2344
2345 garray_T *instr = &cctx->ctx_instr;
2346 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2347
2348 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
2349 // ISN_STORENR.
2350 // And "var = 0" does not need any instruction.
2351 if (lhs->lhs_lvar->lv_from_outer == 0
2352 && instr->ga_len == instr_count + 1
2353 && isn->isn_type == ISN_PUSHNR)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002354 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002355 varnumber_T val = isn->isn_arg.number;
2356 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002357
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002358 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002359 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002360 // zero is the default value, no need to do anything
2361 --instr->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002362 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002363 else
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002364 {
2365 isn->isn_type = ISN_STORENR;
2366 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
2367 isn->isn_arg.storenr.stnr_val = val;
2368 }
2369 if (stack->ga_len > 0)
2370 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002371 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002372 else if (lhs->lhs_lvar->lv_from_outer > 0)
2373 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
2374 lhs->lhs_lvar->lv_from_outer, lhs->lhs_lvar->lv_loop_idx);
2375 else
2376 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002377 return OK;
2378}
2379
2380#if defined(FEAT_PROFILE) || defined(PROTO)
2381 void
2382may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2383{
2384 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
2385 generate_instr(cctx, ISN_PROF_END);
2386}
2387#endif
2388
2389
2390/*
2391 * Delete an instruction, free what it contains.
2392 */
2393 void
2394delete_instr(isn_T *isn)
2395{
2396 switch (isn->isn_type)
2397 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002398 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002399 case ISN_DEF:
2400 case ISN_EXEC:
2401 case ISN_EXECRANGE:
2402 case ISN_EXEC_SPLIT:
2403 case ISN_LEGACY_EVAL:
2404 case ISN_LOADAUTO:
2405 case ISN_LOADB:
2406 case ISN_LOADENV:
2407 case ISN_LOADG:
2408 case ISN_LOADOPT:
2409 case ISN_LOADT:
2410 case ISN_LOADW:
2411 case ISN_LOCKUNLOCK:
2412 case ISN_PUSHEXC:
2413 case ISN_PUSHFUNC:
2414 case ISN_PUSHS:
2415 case ISN_RANGE:
2416 case ISN_STOREAUTO:
2417 case ISN_STOREB:
2418 case ISN_STOREENV:
2419 case ISN_STOREG:
2420 case ISN_STORET:
2421 case ISN_STOREW:
2422 case ISN_STRINGMEMBER:
2423 vim_free(isn->isn_arg.string);
2424 break;
2425
2426 case ISN_SUBSTITUTE:
2427 {
2428 int idx;
2429 isn_T *list = isn->isn_arg.subs.subs_instr;
2430
2431 vim_free(isn->isn_arg.subs.subs_cmd);
2432 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2433 delete_instr(list + idx);
2434 vim_free(list);
2435 }
2436 break;
2437
2438 case ISN_INSTR:
2439 {
2440 int idx;
2441 isn_T *list = isn->isn_arg.instr;
2442
2443 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2444 delete_instr(list + idx);
2445 vim_free(list);
2446 }
2447 break;
2448
2449 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002450 case ISN_LOADEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002451 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002452 case ISN_STOREEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002453 vim_free(isn->isn_arg.loadstore.ls_name);
2454 break;
2455
2456 case ISN_UNLET:
2457 case ISN_UNLETENV:
2458 vim_free(isn->isn_arg.unlet.ul_name);
2459 break;
2460
2461 case ISN_STOREOPT:
2462 case ISN_STOREFUNCOPT:
2463 vim_free(isn->isn_arg.storeopt.so_name);
2464 break;
2465
2466 case ISN_PUSHBLOB: // push blob isn_arg.blob
2467 blob_unref(isn->isn_arg.blob);
2468 break;
2469
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002470 case ISN_UCALL:
2471 vim_free(isn->isn_arg.ufunc.cuf_name);
2472 break;
2473
2474 case ISN_FUNCREF:
2475 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002476 funcref_T *funcref = &isn->isn_arg.funcref;
2477 funcref_extra_T *extra = funcref->fr_extra;
2478
2479 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002480 {
2481 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002482 + funcref->fr_dfunc_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002483 ufunc_T *ufunc = dfunc->df_ufunc;
2484
2485 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2486 func_ptr_unref(ufunc);
2487 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002488 if (extra != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002489 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002490 char_u *name = extra->fre_func_name;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002491
2492 if (name != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002493 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002494 func_unref(name);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002495 vim_free(name);
2496 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00002497 if (extra->fre_class != NULL)
2498 class_unref(extra->fre_class);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002499 vim_free(extra);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002500 }
2501 }
2502 break;
2503
2504 case ISN_DCALL:
2505 {
2506 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002507 + isn->isn_arg.dfunc.cdf_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002508
2509 if (dfunc->df_ufunc != NULL
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002510 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002511 func_ptr_unref(dfunc->df_ufunc);
2512 }
2513 break;
2514
Bram Moolenaard0200c82023-01-28 15:19:40 +00002515 case ISN_METHODCALL:
2516 {
2517 cmfunc_T *mfunc = isn->isn_arg.mfunc;
2518 class_unref(mfunc->cmf_itf);
2519 vim_free(mfunc);
2520 }
2521 break;
2522
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002523 case ISN_NEWFUNC:
2524 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002525 newfuncarg_T *arg = isn->isn_arg.newfunc.nf_arg;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002526
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002527 if (arg != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002528 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002529 ufunc_T *ufunc = find_func_even_dead(
2530 arg->nfa_lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002531
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002532 if (ufunc != NULL)
2533 {
2534 unlink_def_function(ufunc);
2535 func_ptr_unref(ufunc);
2536 }
2537
2538 vim_free(arg->nfa_lambda);
2539 vim_free(arg->nfa_global);
2540 vim_free(arg);
2541 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002542 }
2543 break;
2544
2545 case ISN_CHECKTYPE:
2546 case ISN_SETTYPE:
2547 free_type(isn->isn_arg.type.ct_type);
2548 break;
2549
2550 case ISN_CMDMOD:
2551 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002552 ->cmod_filter_regmatch.regprog);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002553 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2554 break;
2555
2556 case ISN_LOADSCRIPT:
2557 case ISN_STORESCRIPT:
2558 vim_free(isn->isn_arg.script.scriptref);
2559 break;
2560
Bram Moolenaard505d172022-12-18 21:42:55 +00002561 case ISN_LOAD_CLASSMEMBER:
2562 case ISN_STORE_CLASSMEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00002563 case ISN_GET_ITF_MEMBER:
Bram Moolenaard505d172022-12-18 21:42:55 +00002564 class_unref(isn->isn_arg.classmember.cm_class);
2565 break;
2566
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002567 case ISN_STOREINDEX:
2568 class_unref(isn->isn_arg.storeindex.si_class);
2569 break;
2570
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002571 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002572 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002573 break;
2574
2575 case ISN_CEXPR_CORE:
2576 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2577 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2578 break;
2579
2580 case ISN_2BOOL:
2581 case ISN_2STRING:
2582 case ISN_2STRING_ANY:
2583 case ISN_ADDBLOB:
2584 case ISN_ADDLIST:
2585 case ISN_ANYINDEX:
2586 case ISN_ANYSLICE:
2587 case ISN_BCALL:
2588 case ISN_BLOBAPPEND:
2589 case ISN_BLOBINDEX:
2590 case ISN_BLOBSLICE:
2591 case ISN_CATCH:
2592 case ISN_CEXPR_AUCMD:
2593 case ISN_CHECKLEN:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002594 case ISN_CLEARDICT:
2595 case ISN_CMDMOD_REV:
2596 case ISN_COMPAREANY:
2597 case ISN_COMPAREBLOB:
2598 case ISN_COMPAREBOOL:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002599 case ISN_COMPARECLASS:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002600 case ISN_COMPAREDICT:
2601 case ISN_COMPAREFLOAT:
2602 case ISN_COMPAREFUNC:
2603 case ISN_COMPARELIST:
2604 case ISN_COMPARENR:
Bram Moolenaar7a222242022-03-01 19:23:24 +00002605 case ISN_COMPARENULL:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002606 case ISN_COMPAREOBJECT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002607 case ISN_COMPARESPECIAL:
2608 case ISN_COMPARESTRING:
2609 case ISN_CONCAT:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002610 case ISN_CONSTRUCT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002611 case ISN_COND2BOOL:
2612 case ISN_DEBUG:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002613 case ISN_DEFER:
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00002614 case ISN_DEFEROBJ:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002615 case ISN_DROP:
2616 case ISN_ECHO:
2617 case ISN_ECHOCONSOLE:
2618 case ISN_ECHOERR:
2619 case ISN_ECHOMSG:
Bram Moolenaar68a635a2022-09-01 17:26:17 +01002620 case ISN_ECHOWINDOW:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002621 case ISN_ENDLOOP:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002622 case ISN_ENDTRY:
2623 case ISN_EXECCONCAT:
2624 case ISN_EXECUTE:
2625 case ISN_FINALLY:
2626 case ISN_FINISH:
2627 case ISN_FOR:
2628 case ISN_GETITEM:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002629 case ISN_GET_OBJ_MEMBER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002630 case ISN_JUMP:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002631 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002632 case ISN_JUMP_IF_ARG_SET:
2633 case ISN_LISTAPPEND:
2634 case ISN_LISTINDEX:
2635 case ISN_LISTSLICE:
2636 case ISN_LOAD:
2637 case ISN_LOADBDICT:
2638 case ISN_LOADGDICT:
2639 case ISN_LOADOUTER:
2640 case ISN_LOADREG:
2641 case ISN_LOADTDICT:
2642 case ISN_LOADV:
2643 case ISN_LOADWDICT:
2644 case ISN_LOCKCONST:
2645 case ISN_MEMBER:
2646 case ISN_NEGATENR:
2647 case ISN_NEWDICT:
2648 case ISN_NEWLIST:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002649 case ISN_NEWPARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002650 case ISN_OPANY:
2651 case ISN_OPFLOAT:
2652 case ISN_OPNR:
2653 case ISN_PCALL:
2654 case ISN_PCALL_END:
2655 case ISN_PROF_END:
2656 case ISN_PROF_START:
2657 case ISN_PUSHBOOL:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002658 case ISN_PUSHCHANNEL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002659 case ISN_PUSHF:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002660 case ISN_PUSHJOB:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002661 case ISN_PUSHNR:
2662 case ISN_PUSHSPEC:
2663 case ISN_PUT:
2664 case ISN_REDIREND:
2665 case ISN_REDIRSTART:
2666 case ISN_RETURN:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002667 case ISN_RETURN_OBJECT:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002668 case ISN_RETURN_VOID:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002669 case ISN_SHUFFLE:
2670 case ISN_SLICE:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002671 case ISN_SOURCE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002672 case ISN_STORE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002673 case ISN_STORENR:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002674 case ISN_STOREOUTER:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002675 case ISN_STORE_THIS:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002676 case ISN_STORERANGE:
2677 case ISN_STOREREG:
2678 case ISN_STOREV:
2679 case ISN_STRINDEX:
2680 case ISN_STRSLICE:
2681 case ISN_THROW:
2682 case ISN_TRYCONT:
2683 case ISN_UNLETINDEX:
2684 case ISN_UNLETRANGE:
2685 case ISN_UNPACK:
2686 case ISN_USEDICT:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002687 case ISN_WHILE:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002688 // nothing allocated
2689 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002690 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002691}
2692
2693 void
2694clear_instr_ga(garray_T *gap)
2695{
2696 int idx;
2697
2698 for (idx = 0; idx < gap->ga_len; ++idx)
2699 delete_instr(((isn_T *)gap->ga_data) + idx);
2700 ga_clear(gap);
2701}
2702
2703
2704#endif // defined(FEAT_EVAL)