blob: f7b074c79abfecf87be40e25e55d0c52a3cd379d [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
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +0200139generate_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
Ernie Rael18143d32023-09-04 22:30:41 +0200148 isn->isn_arg.classmember.cm_class = NULL;
149 isn->isn_arg.classmember.cm_idx = idx;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000150 return push_type_stack2(cctx, type, &t_any);
151}
152
153/*
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000154 * Generate ISN_GET_ITF_MEMBER - access member of interface at bottom of stack
155 * by index.
156 */
157 int
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +0200158generate_GET_ITF_MEMBER(cctx_T *cctx, class_T *itf, int idx, type_T *type)
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000159{
160 RETURN_OK_IF_SKIP(cctx);
161
162 // drop the object type
163 isn_T *isn = generate_instr_drop(cctx, ISN_GET_ITF_MEMBER, 1);
164 if (isn == NULL)
165 return FAIL;
166
167 isn->isn_arg.classmember.cm_class = itf;
168 ++itf->class_refcount;
169 isn->isn_arg.classmember.cm_idx = idx;
170 return push_type_stack2(cctx, type, &t_any);
171}
172
173/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000174 * Generate ISN_STORE_THIS - store value in member of "this" object with member
175 * index "idx".
176 */
177 int
178generate_STORE_THIS(cctx_T *cctx, int idx)
179{
180 RETURN_OK_IF_SKIP(cctx);
181
182 // drop the value type
183 isn_T *isn = generate_instr_drop(cctx, ISN_STORE_THIS, 1);
184 if (isn == NULL)
185 return FAIL;
186
187 isn->isn_arg.number = idx;
188 return OK;
189}
190
191/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000192 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
193 * But only for simple types.
194 * When "tolerant" is TRUE convert most types to string, e.g. a List.
195 */
196 int
197may_generate_2STRING(int offset, int tolerant, cctx_T *cctx)
198{
199 isn_T *isn;
200 isntype_T isntype = ISN_2STRING;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000201 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000202
203 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +0000204 type = get_type_on_stack(cctx, -1 - offset);
205 switch (type->tt_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000206 {
207 // nothing to be done
208 case VAR_STRING: return OK;
209
210 // conversion possible
211 case VAR_SPECIAL:
212 case VAR_BOOL:
213 case VAR_NUMBER:
214 case VAR_FLOAT:
215 break;
216
217 // conversion possible (with runtime check)
218 case VAR_ANY:
219 case VAR_UNKNOWN:
220 isntype = ISN_2STRING_ANY;
221 break;
222
223 // conversion possible when tolerant
224 case VAR_LIST:
225 if (tolerant)
226 {
227 isntype = ISN_2STRING_ANY;
228 break;
229 }
230 // FALLTHROUGH
231
232 // conversion not possible
233 case VAR_VOID:
234 case VAR_BLOB:
235 case VAR_FUNC:
236 case VAR_PARTIAL:
237 case VAR_DICT:
238 case VAR_JOB:
239 case VAR_CHANNEL:
240 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000241 case VAR_CLASS:
242 case VAR_OBJECT:
Bram Moolenaar078a4612022-01-04 15:17:03 +0000243 to_string_error(type->tt_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000244 return FAIL;
245 }
246
Bram Moolenaar078a4612022-01-04 15:17:03 +0000247 set_type_on_stack(cctx, &t_string, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000248 if ((isn = generate_instr(cctx, isntype)) == NULL)
249 return FAIL;
250 isn->isn_arg.tostring.offset = offset;
251 isn->isn_arg.tostring.tolerant = tolerant;
252
253 return OK;
254}
255
256 static int
257check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
258{
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000259 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT
260 || type1 == VAR_ANY || type1 == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000261 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000262 || type2 == VAR_ANY || type2 == VAR_UNKNOWN)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000263 {
264 if (*op == '+')
265 emsg(_(e_wrong_argument_type_for_plus));
266 else
267 semsg(_(e_char_requires_number_or_float_arguments), *op);
268 return FAIL;
269 }
270 return OK;
271}
272
273/*
274 * Generate instruction for "+". For a list this creates a new list.
275 */
276 int
277generate_add_instr(
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000278 cctx_T *cctx,
279 vartype_T vartype,
280 type_T *type1,
281 type_T *type2,
282 exprtype_T expr_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000283{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000284 isn_T *isn = generate_instr_drop(cctx,
285 vartype == VAR_NUMBER ? ISN_OPNR
286 : vartype == VAR_LIST ? ISN_ADDLIST
287 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000288 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000289 : ISN_OPANY, 1);
290
291 if (vartype != VAR_LIST && vartype != VAR_BLOB
292 && type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000293 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000294 && type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000295 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000296 && check_number_or_float(
297 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
298 return FAIL;
299
300 if (isn != NULL)
301 {
302 if (isn->isn_type == ISN_ADDLIST)
303 isn->isn_arg.op.op_type = expr_type;
304 else
305 isn->isn_arg.op.op_type = EXPR_ADD;
306 }
307
308 // When concatenating two lists with different member types the member type
309 // becomes "any".
310 if (vartype == VAR_LIST
311 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
312 && type1->tt_member != type2->tt_member)
Bram Moolenaar078a4612022-01-04 15:17:03 +0000313 set_type_on_stack(cctx, &t_list_any, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000314
315 return isn == NULL ? FAIL : OK;
316}
317
318/*
319 * Get the type to use for an instruction for an operation on "type1" and
320 * "type2". If they are matching use a type-specific instruction. Otherwise
321 * fall back to runtime type checking.
322 */
323 vartype_T
324operator_type(type_T *type1, type_T *type2)
325{
326 if (type1->tt_type == type2->tt_type
327 && (type1->tt_type == VAR_NUMBER
328 || type1->tt_type == VAR_LIST
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000329 || type1->tt_type == VAR_FLOAT
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000330 || type1->tt_type == VAR_BLOB))
331 return type1->tt_type;
332 return VAR_ANY;
333}
334
335/*
336 * Generate an instruction with two arguments. The instruction depends on the
337 * type of the arguments.
338 */
339 int
340generate_two_op(cctx_T *cctx, char_u *op)
341{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000342 type_T *type1;
343 type_T *type2;
344 vartype_T vartype;
345 isn_T *isn;
346
347 RETURN_OK_IF_SKIP(cctx);
348
349 // Get the known type of the two items on the stack.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000350 type1 = get_type_on_stack(cctx, 1);
351 type2 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000352 vartype = operator_type(type1, type2);
353
354 switch (*op)
355 {
356 case '+':
357 if (generate_add_instr(cctx, vartype, type1, type2,
358 EXPR_COPY) == FAIL)
359 return FAIL;
360 break;
361
362 case '-':
363 case '*':
364 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
365 op) == FAIL)
366 return FAIL;
367 if (vartype == VAR_NUMBER)
368 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000369 else if (vartype == VAR_FLOAT)
370 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000371 else
372 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
373 if (isn != NULL)
374 isn->isn_arg.op.op_type = *op == '*'
375 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
376 break;
377
378 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000379 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000380 && type1->tt_type != VAR_NUMBER)
381 || (type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000382 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000383 && type2->tt_type != VAR_NUMBER))
384 {
385 emsg(_(e_percent_requires_number_arguments));
386 return FAIL;
387 }
388 isn = generate_instr_drop(cctx,
389 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
390 if (isn != NULL)
391 isn->isn_arg.op.op_type = EXPR_REM;
392 break;
393 }
394
395 // correct type of result
396 if (vartype == VAR_ANY)
397 {
398 type_T *type = &t_any;
399
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000400 // float+number and number+float results in float
401 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100402 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000403 type = &t_float;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000404 set_type_on_stack(cctx, type, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000405 }
406
407 return OK;
408}
409
410/*
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000411 * Get the instruction to use for comparing two values with specified types.
412 * Either "tv1" and "tv2" are passed or "type1" and "type2".
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000413 * Return ISN_DROP when failed.
414 */
415 static isntype_T
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000416get_compare_isn(
Bram Moolenaar2ed57ac2023-04-01 22:05:38 +0100417 exprtype_T exprtype,
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000418 typval_T *tv1,
419 typval_T *tv2,
420 type_T *type1,
421 type_T *type2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000422{
423 isntype_T isntype = ISN_DROP;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000424 vartype_T vartype1 = tv1 != NULL ? tv1->v_type : type1->tt_type;
425 vartype_T vartype2 = tv2 != NULL ? tv2->v_type : type2->tt_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000426
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000427 if (vartype1 == vartype2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000428 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000429 switch (vartype1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000430 {
431 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
432 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
433 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
434 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
435 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
436 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
437 case VAR_LIST: isntype = ISN_COMPARELIST; break;
438 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
439 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000440 case VAR_CLASS: isntype = ISN_COMPARECLASS; break;
441 case VAR_OBJECT: isntype = ISN_COMPAREOBJECT; break;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000442 default: isntype = ISN_COMPAREANY; break;
443 }
444 }
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000445 else if (vartype1 == VAR_ANY || vartype2 == VAR_ANY
446 || ((vartype1 == VAR_NUMBER || vartype1 == VAR_FLOAT)
447 && (vartype2 == VAR_NUMBER || vartype2 == VAR_FLOAT))
448 || (vartype1 == VAR_FUNC && vartype2 == VAR_PARTIAL)
449 || (vartype1 == VAR_PARTIAL && vartype2 == VAR_FUNC))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000450 isntype = ISN_COMPAREANY;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000451 else if (vartype1 == VAR_SPECIAL || vartype2 == VAR_SPECIAL)
Bram Moolenaar7a222242022-03-01 19:23:24 +0000452 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000453 if ((vartype1 == VAR_SPECIAL
454 && (tv1 != NULL ? tv1->vval.v_number == VVAL_NONE
455 : type1 == &t_none)
456 && vartype2 != VAR_STRING)
457 || (vartype2 == VAR_SPECIAL
458 && (tv2 != NULL ? tv2->vval.v_number == VVAL_NONE
459 : type2 == &t_none)
460 && vartype1 != VAR_STRING))
461 {
462 semsg(_(e_cannot_compare_str_with_str),
463 vartype_name(vartype1), vartype_name(vartype2));
464 return ISN_DROP;
465 }
Bram Moolenaar05667812022-03-15 20:21:33 +0000466 // although comparing null with number, float or bool is not useful, we
467 // allow it
Bram Moolenaar7a222242022-03-01 19:23:24 +0000468 isntype = ISN_COMPARENULL;
469 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000470
471 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
472 && (isntype == ISN_COMPAREBOOL
473 || isntype == ISN_COMPARESPECIAL
474 || isntype == ISN_COMPARENR
475 || isntype == ISN_COMPAREFLOAT))
476 {
477 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000478 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(vartype1));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000479 return ISN_DROP;
480 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000481 if (!(exprtype == EXPR_IS || exprtype == EXPR_ISNOT
482 || exprtype == EXPR_EQUAL || exprtype == EXPR_NEQUAL)
483 && (isntype == ISN_COMPAREOBJECT || isntype == ISN_COMPARECLASS))
484 {
485 semsg(_(e_invalid_operation_for_str), vartype_name(vartype1));
486 return ISN_DROP;
487 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000488 if (isntype == ISN_DROP
Bram Moolenaar2ed57ac2023-04-01 22:05:38 +0100489 || (isntype != ISN_COMPARENULL
490 && (((exprtype != EXPR_EQUAL
491 && exprtype != EXPR_NEQUAL
492 && (vartype1 == VAR_BOOL || vartype1 == VAR_SPECIAL
493 || vartype2 == VAR_BOOL || vartype2 == VAR_SPECIAL)))
494 || ((exprtype != EXPR_EQUAL
495 && exprtype != EXPR_NEQUAL
496 && exprtype != EXPR_IS
497 && exprtype != EXPR_ISNOT
498 && (vartype1 == VAR_BLOB || vartype2 == VAR_BLOB
499 || vartype1 == VAR_LIST || vartype2 == VAR_LIST))))))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000500 {
501 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000502 vartype_name(vartype1), vartype_name(vartype2));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000503 return ISN_DROP;
504 }
505 return isntype;
506}
507
508 int
509check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
510{
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000511 if (get_compare_isn(type, tv1, tv2, NULL, NULL) == ISN_DROP)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000512 return FAIL;
513 return OK;
514}
515
516/*
517 * Generate an ISN_COMPARE* instruction with a boolean result.
518 */
519 int
520generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
521{
522 isntype_T isntype;
523 isn_T *isn;
524 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000525
526 RETURN_OK_IF_SKIP(cctx);
527
528 // Get the known type of the two items on the stack. If they are matching
529 // use a type-specific instruction. Otherwise fall back to runtime type
530 // checking.
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000531 isntype = get_compare_isn(exprtype, NULL, NULL,
532 get_type_on_stack(cctx, 1), get_type_on_stack(cctx, 0));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000533 if (isntype == ISN_DROP)
534 return FAIL;
535
536 if ((isn = generate_instr(cctx, isntype)) == NULL)
537 return FAIL;
538 isn->isn_arg.op.op_type = exprtype;
539 isn->isn_arg.op.op_ic = ic;
540
541 // takes two arguments, puts one bool back
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100542 --stack->ga_len;
543 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000544
545 return OK;
546}
547
548/*
LemonBoy372bcce2022-04-25 12:43:20 +0100549 * Generate an ISN_CONCAT instruction.
550 * "count" is the number of stack elements to join together and it must be
551 * greater or equal to one.
552 * The caller ensures all the "count" elements on the stack have the right type.
553 */
554 int
555generate_CONCAT(cctx_T *cctx, int count)
556{
557 isn_T *isn;
558 garray_T *stack = &cctx->ctx_type_stack;
559
560 RETURN_OK_IF_SKIP(cctx);
561
LemonBoy372bcce2022-04-25 12:43:20 +0100562 if ((isn = generate_instr(cctx, ISN_CONCAT)) == NULL)
563 return FAIL;
564 isn->isn_arg.number = count;
565
566 // drop the argument types
567 stack->ga_len -= count - 1;
568
569 return OK;
570}
571
572/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000573 * Generate an ISN_2BOOL instruction.
574 * "offset" is the offset in the type stack.
575 */
576 int
577generate_2BOOL(cctx_T *cctx, int invert, int offset)
578{
579 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000580
581 RETURN_OK_IF_SKIP(cctx);
582 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
583 return FAIL;
584 isn->isn_arg.tobool.invert = invert;
585 isn->isn_arg.tobool.offset = offset;
586
587 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000588 set_type_on_stack(cctx, &t_bool, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000589
590 return OK;
591}
592
593/*
594 * Generate an ISN_COND2BOOL instruction.
595 */
596 int
597generate_COND2BOOL(cctx_T *cctx)
598{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000599 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100600 if (generate_instr(cctx, ISN_COND2BOOL) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000601 return FAIL;
602
603 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000604 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000605
606 return OK;
607}
608
609 int
610generate_TYPECHECK(
611 cctx_T *cctx,
612 type_T *expected,
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000613 int number_ok, // add TTFLAG_NUMBER_OK flag
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000614 int offset,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100615 int is_var,
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000616 int argidx)
617{
618 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000619
620 RETURN_OK_IF_SKIP(cctx);
621 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
622 return FAIL;
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000623 type_T *tt;
624 if (expected->tt_type == VAR_FLOAT && number_ok)
625 {
626 // always allocate, also for static types
627 tt = ALLOC_ONE(type_T);
628 if (tt != NULL)
629 {
630 *tt = *expected;
Bram Moolenaarc4b3f642022-12-30 10:36:34 +0000631 tt->tt_flags &= ~TTFLAG_STATIC;
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000632 tt->tt_flags |= TTFLAG_NUMBER_OK;
633 }
634 }
635 else
636 tt = alloc_type(expected);
637
638 isn->isn_arg.type.ct_type = tt;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000639 isn->isn_arg.type.ct_off = (int8_T)offset;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100640 isn->isn_arg.type.ct_is_var = is_var;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000641 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
642
643 // type becomes expected
Bram Moolenaar078a4612022-01-04 15:17:03 +0000644 set_type_on_stack(cctx, expected, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000645
646 return OK;
647}
648
649 int
650generate_SETTYPE(
651 cctx_T *cctx,
652 type_T *expected)
653{
654 isn_T *isn;
655
656 RETURN_OK_IF_SKIP(cctx);
657 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
658 return FAIL;
659 isn->isn_arg.type.ct_type = alloc_type(expected);
660 return OK;
661}
662
663/*
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000664 * Generate an ISN_PUSHOBJ instruction. Object is always NULL.
665 */
Ernie Rael5c018be2023-08-27 18:40:26 +0200666 int
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000667generate_PUSHOBJ(cctx_T *cctx)
668{
669 RETURN_OK_IF_SKIP(cctx);
Ernie Rael5c018be2023-08-27 18:40:26 +0200670 if (generate_instr_type(cctx, ISN_PUSHOBJ, &t_object) == NULL)
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000671 return FAIL;
672 return OK;
673}
674
675/*
676 * Generate an ISN_PUSHCLASS instruction. "class" can be NULL.
677 */
678 static int
679generate_PUSHCLASS(cctx_T *cctx, class_T *class)
680{
681 RETURN_OK_IF_SKIP(cctx);
682 isn_T *isn = generate_instr_type(cctx, ISN_PUSHCLASS,
683 class == NULL ? &t_any : &class->class_type);
684 if (isn == NULL)
685 return FAIL;
Bram Moolenaar30a84472023-02-27 08:07:14 +0000686 isn->isn_arg.classarg = class;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000687 if (class != NULL)
688 ++class->class_refcount;
689 return OK;
690}
691
692/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000693 * Generate a PUSH instruction for "tv".
694 * "tv" will be consumed or cleared.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000695 */
696 int
697generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
698{
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100699 switch (tv->v_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000700 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100701 case VAR_BOOL:
702 generate_PUSHBOOL(cctx, tv->vval.v_number);
703 break;
704 case VAR_SPECIAL:
705 generate_PUSHSPEC(cctx, tv->vval.v_number);
706 break;
707 case VAR_NUMBER:
708 generate_PUSHNR(cctx, tv->vval.v_number);
709 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100710 case VAR_FLOAT:
711 generate_PUSHF(cctx, tv->vval.v_float);
712 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100713 case VAR_BLOB:
714 generate_PUSHBLOB(cctx, tv->vval.v_blob);
715 tv->vval.v_blob = NULL;
716 break;
717 case VAR_LIST:
718 if (tv->vval.v_list != NULL)
719 iemsg("non-empty list constant not supported");
720 generate_NEWLIST(cctx, 0, TRUE);
721 break;
722 case VAR_DICT:
723 if (tv->vval.v_dict != NULL)
724 iemsg("non-empty dict constant not supported");
725 generate_NEWDICT(cctx, 0, TRUE);
726 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000727#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100728 case VAR_JOB:
729 if (tv->vval.v_job != NULL)
730 iemsg("non-null job constant not supported");
731 generate_PUSHJOB(cctx);
732 break;
733 case VAR_CHANNEL:
734 if (tv->vval.v_channel != NULL)
735 iemsg("non-null channel constant not supported");
736 generate_PUSHCHANNEL(cctx);
737 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000738#endif
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100739 case VAR_FUNC:
740 if (tv->vval.v_string != NULL)
741 iemsg("non-null function constant not supported");
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100742 generate_PUSHFUNC(cctx, NULL, &t_func_unknown, TRUE);
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100743 break;
744 case VAR_PARTIAL:
745 if (tv->vval.v_partial != NULL)
746 iemsg("non-null partial constant not supported");
747 if (generate_instr_type(cctx, ISN_NEWPARTIAL, &t_func_unknown)
748 == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000749 return FAIL;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100750 break;
751 case VAR_STRING:
752 generate_PUSHS(cctx, &tv->vval.v_string);
753 tv->vval.v_string = NULL;
754 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000755 case VAR_OBJECT:
756 if (tv->vval.v_object != NULL)
757 {
758 emsg(_(e_cannot_use_non_null_object));
759 return FAIL;
760 }
761 generate_PUSHOBJ(cctx);
762 break;
763 case VAR_CLASS:
764 generate_PUSHCLASS(cctx, tv->vval.v_class);
765 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100766 default:
767 siemsg("constant type %d not supported", tv->v_type);
768 clear_tv(tv);
769 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000770 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100771 tv->v_type = VAR_UNKNOWN;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000772 return OK;
773}
774
775/*
776 * Generate an ISN_PUSHNR instruction.
777 */
778 int
779generate_PUSHNR(cctx_T *cctx, varnumber_T number)
780{
781 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000782
783 RETURN_OK_IF_SKIP(cctx);
784 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
785 return FAIL;
786 isn->isn_arg.number = number;
787
788 if (number == 0 || number == 1)
789 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000790 set_type_on_stack(cctx, &t_number_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000791 return OK;
792}
793
794/*
795 * Generate an ISN_PUSHBOOL instruction.
796 */
797 int
798generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
799{
800 isn_T *isn;
801
802 RETURN_OK_IF_SKIP(cctx);
803 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
804 return FAIL;
805 isn->isn_arg.number = number;
806
807 return OK;
808}
809
810/*
811 * Generate an ISN_PUSHSPEC instruction.
812 */
813 int
814generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
815{
816 isn_T *isn;
817
818 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000819 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC,
820 number == VVAL_NULL ? &t_null : &t_none)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000821 return FAIL;
822 isn->isn_arg.number = number;
823
824 return OK;
825}
826
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000827/*
828 * Generate an ISN_PUSHF instruction.
829 */
830 int
831generate_PUSHF(cctx_T *cctx, float_T fnumber)
832{
833 isn_T *isn;
834
835 RETURN_OK_IF_SKIP(cctx);
836 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
837 return FAIL;
838 isn->isn_arg.fnumber = fnumber;
839
840 return OK;
841}
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000842
843/*
844 * Generate an ISN_PUSHS instruction.
845 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100846 * Note that if "str" is used in the instruction OK is returned and "*str" is
847 * not set to NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000848 */
849 int
850generate_PUSHS(cctx_T *cctx, char_u **str)
851{
852 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100853 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000854
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100855 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000856 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100857 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
858 ret = FAIL;
859 else
860 {
861 isn->isn_arg.string = str == NULL ? NULL : *str;
862 return OK;
863 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000864 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100865 if (str != NULL)
866 VIM_CLEAR(*str);
867 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000868}
869
870/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000871 * Generate an ISN_PUSHCHANNEL instruction. Channel is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000872 */
873 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000874generate_PUSHCHANNEL(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000875{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000876 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100877#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100878 if (generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000879 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000880 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100881#else
882 emsg(_(e_channel_job_feature_not_available));
883 return FAIL;
884#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000885}
886
887/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000888 * Generate an ISN_PUSHJOB instruction. Job is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000889 */
890 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000891generate_PUSHJOB(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000892{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000893 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100894#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100895 if (generate_instr_type(cctx, ISN_PUSHJOB, &t_job) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000896 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000897 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100898#else
899 emsg(_(e_channel_job_feature_not_available));
900 return FAIL;
901#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000902}
903
904/*
905 * Generate an ISN_PUSHBLOB instruction.
906 * Consumes "blob".
907 */
908 int
909generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
910{
911 isn_T *isn;
912
913 RETURN_OK_IF_SKIP(cctx);
914 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
915 return FAIL;
916 isn->isn_arg.blob = blob;
917
918 return OK;
919}
920
921/*
922 * Generate an ISN_PUSHFUNC instruction with name "name".
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100923 * When "may_prefix" is TRUE prefix "g:" unless "name" is script-local or
924 * autoload.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000925 */
926 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100927generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type, int may_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000928{
929 isn_T *isn;
930 char_u *funcname;
931
932 RETURN_OK_IF_SKIP(cctx);
933 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
934 return FAIL;
935 if (name == NULL)
936 funcname = NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100937 else if (!may_prefix
938 || *name == K_SPECIAL // script-local
Bram Moolenaard041f422022-01-12 19:54:00 +0000939 || vim_strchr(name, AUTOLOAD_CHAR) != NULL) // autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000940 funcname = vim_strsave(name);
941 else
942 {
943 funcname = alloc(STRLEN(name) + 3);
944 if (funcname != NULL)
945 {
946 STRCPY(funcname, "g:");
947 STRCPY(funcname + 2, name);
948 }
949 }
950
951 isn->isn_arg.string = funcname;
952 return OK;
953}
954
955/*
Bram Moolenaar06b77222022-01-25 15:51:56 +0000956 * Generate an ISN_AUTOLOAD instruction.
957 */
958 int
959generate_AUTOLOAD(cctx_T *cctx, char_u *name, type_T *type)
960{
961 isn_T *isn;
962
963 RETURN_OK_IF_SKIP(cctx);
964 if ((isn = generate_instr_type(cctx, ISN_AUTOLOAD, type)) == NULL)
965 return FAIL;
966 isn->isn_arg.string = vim_strsave(name);
967 if (isn->isn_arg.string == NULL)
968 return FAIL;
969 return OK;
970}
971
972/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000973 * Generate an ISN_GETITEM instruction with "index".
974 * "with_op" is TRUE for "+=" and other operators, the stack has the current
975 * value below the list with values.
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100976 * Caller must check the type is a list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000977 */
978 int
979generate_GETITEM(cctx_T *cctx, int index, int with_op)
980{
981 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000982 type_T *type = get_type_on_stack(cctx, with_op ? 1 : 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000983 type_T *item_type = &t_any;
984
985 RETURN_OK_IF_SKIP(cctx);
986
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000987 item_type = type->tt_member;
988 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
989 return FAIL;
990 isn->isn_arg.getitem.gi_index = index;
991 isn->isn_arg.getitem.gi_with_op = with_op;
992
993 // add the item type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +0000994 return push_type_stack(cctx, item_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000995}
996
997/*
998 * Generate an ISN_SLICE instruction with "count".
999 */
1000 int
1001generate_SLICE(cctx_T *cctx, int count)
1002{
1003 isn_T *isn;
1004
1005 RETURN_OK_IF_SKIP(cctx);
1006 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1007 return FAIL;
1008 isn->isn_arg.number = count;
1009 return OK;
1010}
1011
1012/*
1013 * Generate an ISN_CHECKLEN instruction with "min_len".
1014 */
1015 int
1016generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1017{
1018 isn_T *isn;
1019
1020 RETURN_OK_IF_SKIP(cctx);
1021
1022 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1023 return FAIL;
1024 isn->isn_arg.checklen.cl_min_len = min_len;
1025 isn->isn_arg.checklen.cl_more_OK = more_OK;
1026
1027 return OK;
1028}
1029
1030/*
1031 * Generate an ISN_STORE instruction.
1032 */
1033 int
1034generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1035{
1036 isn_T *isn;
1037
1038 RETURN_OK_IF_SKIP(cctx);
1039 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1040 return FAIL;
1041 if (name != NULL)
1042 isn->isn_arg.string = vim_strsave(name);
1043 else
1044 isn->isn_arg.number = idx;
1045
1046 return OK;
1047}
1048
1049/*
Bram Moolenaard505d172022-12-18 21:42:55 +00001050 * Generate an ISN_LOAD_CLASSMEMBER ("load" == TRUE) or ISN_STORE_CLASSMEMBER
1051 * ("load" == FALSE) instruction.
1052 */
1053 int
1054generate_CLASSMEMBER(
1055 cctx_T *cctx,
1056 int load,
1057 class_T *cl,
1058 int idx)
1059{
1060 isn_T *isn;
1061
1062 RETURN_OK_IF_SKIP(cctx);
1063 if (load)
1064 {
1065 ocmember_T *m = &cl->class_class_members[idx];
1066 isn = generate_instr_type(cctx, ISN_LOAD_CLASSMEMBER, m->ocm_type);
1067 }
1068 else
1069 {
1070 isn = generate_instr_drop(cctx, ISN_STORE_CLASSMEMBER, 1);
1071 }
1072 if (isn == NULL)
1073 return FAIL;
1074 isn->isn_arg.classmember.cm_class = cl;
1075 ++cl->class_refcount;
1076 isn->isn_arg.classmember.cm_idx = idx;
1077
1078 return OK;
1079}
1080
1081/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001082 * Generate an ISN_STOREOUTER instruction.
1083 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +00001084 static int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001085generate_STOREOUTER(cctx_T *cctx, int idx, int level, int loop_idx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001086{
1087 isn_T *isn;
1088
1089 RETURN_OK_IF_SKIP(cctx);
1090 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1091 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001092 if (level == 1 && loop_idx >= 0 && idx >= loop_idx)
1093 {
1094 // Store a variable defined in a loop. A copy will be made at the end
1095 // of the loop. TODO: how about deeper nesting?
1096 isn->isn_arg.outer.outer_idx = idx - loop_idx;
1097 isn->isn_arg.outer.outer_depth = OUTER_LOOP_DEPTH;
1098 }
1099 else
1100 {
1101 isn->isn_arg.outer.outer_idx = idx;
1102 isn->isn_arg.outer.outer_depth = level;
1103 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001104
1105 return OK;
1106}
1107
1108/*
1109 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1110 */
1111 int
1112generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1113{
1114 isn_T *isn;
1115
1116 RETURN_OK_IF_SKIP(cctx);
1117 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1118 return FAIL;
1119 isn->isn_arg.storenr.stnr_idx = idx;
1120 isn->isn_arg.storenr.stnr_val = value;
1121
1122 return OK;
1123}
1124
1125/*
1126 * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction
1127 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +00001128 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001129generate_STOREOPT(
1130 cctx_T *cctx,
1131 isntype_T isn_type,
1132 char_u *name,
1133 int opt_flags)
1134{
1135 isn_T *isn;
1136
1137 RETURN_OK_IF_SKIP(cctx);
1138 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1139 return FAIL;
1140 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1141 isn->isn_arg.storeopt.so_flags = opt_flags;
1142
1143 return OK;
1144}
1145
1146/*
1147 * Generate an ISN_LOAD or similar instruction.
1148 */
1149 int
1150generate_LOAD(
1151 cctx_T *cctx,
1152 isntype_T isn_type,
1153 int idx,
1154 char_u *name,
1155 type_T *type)
1156{
1157 isn_T *isn;
1158
1159 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001160 if ((isn = generate_instr_type2(cctx, isn_type, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001161 return FAIL;
1162 if (name != NULL)
1163 isn->isn_arg.string = vim_strsave(name);
1164 else
1165 isn->isn_arg.number = idx;
1166
1167 return OK;
1168}
1169
1170/*
1171 * Generate an ISN_LOADOUTER instruction
1172 */
1173 int
1174generate_LOADOUTER(
1175 cctx_T *cctx,
1176 int idx,
1177 int nesting,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001178 int loop_depth,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001179 int loop_idx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001180 type_T *type)
1181{
1182 isn_T *isn;
1183
1184 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001185 if ((isn = generate_instr_type2(cctx, ISN_LOADOUTER, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001186 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001187 if (nesting == 1 && loop_idx >= 0 && idx >= loop_idx)
1188 {
1189 // Load a variable defined in a loop. A copy will be made at the end
Bram Moolenaarcc341812022-09-19 15:54:34 +01001190 // of the loop.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001191 isn->isn_arg.outer.outer_idx = idx - loop_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001192 isn->isn_arg.outer.outer_depth = -loop_depth - 1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001193 }
1194 else
1195 {
1196 isn->isn_arg.outer.outer_idx = idx;
1197 isn->isn_arg.outer.outer_depth = nesting;
1198 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001199
1200 return OK;
1201}
1202
1203/*
1204 * Generate an ISN_LOADV instruction for v:var.
1205 */
1206 int
1207generate_LOADV(
1208 cctx_T *cctx,
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001209 char_u *name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001210{
1211 int di_flags;
1212 int vidx = find_vim_var(name, &di_flags);
1213 type_T *type;
1214
1215 RETURN_OK_IF_SKIP(cctx);
1216 if (vidx < 0)
1217 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001218 semsg(_(e_variable_not_found_str), name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001219 return FAIL;
1220 }
Bram Moolenaard787e402021-12-24 21:36:12 +00001221 type = get_vim_var_type(vidx, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001222 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
1223}
1224
1225/*
1226 * Generate an ISN_UNLET instruction.
1227 */
1228 int
1229generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
1230{
1231 isn_T *isn;
1232
1233 RETURN_OK_IF_SKIP(cctx);
1234 if ((isn = generate_instr(cctx, isn_type)) == NULL)
1235 return FAIL;
1236 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1237 isn->isn_arg.unlet.ul_forceit = forceit;
1238
1239 return OK;
1240}
1241
1242/*
1243 * Generate an ISN_LOCKCONST instruction.
1244 */
1245 int
1246generate_LOCKCONST(cctx_T *cctx)
1247{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001248 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01001249 if (generate_instr(cctx, ISN_LOCKCONST) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001250 return FAIL;
1251 return OK;
1252}
1253
1254/*
1255 * Generate an ISN_LOADS instruction.
1256 */
1257 int
1258generate_OLDSCRIPT(
1259 cctx_T *cctx,
1260 isntype_T isn_type,
1261 char_u *name,
1262 int sid,
1263 type_T *type)
1264{
1265 isn_T *isn;
1266
1267 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001268 if (isn_type == ISN_LOADS || isn_type == ISN_LOADEXPORT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001269 isn = generate_instr_type(cctx, isn_type, type);
1270 else
1271 isn = generate_instr_drop(cctx, isn_type, 1);
1272 if (isn == NULL)
1273 return FAIL;
1274 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1275 isn->isn_arg.loadstore.ls_sid = sid;
1276
1277 return OK;
1278}
1279
1280/*
1281 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1282 */
1283 int
1284generate_VIM9SCRIPT(
1285 cctx_T *cctx,
1286 isntype_T isn_type,
1287 int sid,
1288 int idx,
1289 type_T *type)
1290{
1291 isn_T *isn;
1292 scriptref_T *sref;
1293 scriptitem_T *si = SCRIPT_ITEM(sid);
1294
1295 RETURN_OK_IF_SKIP(cctx);
1296 if (isn_type == ISN_LOADSCRIPT)
Bram Moolenaar160afdb2022-02-06 17:17:02 +00001297 isn = generate_instr_type2(cctx, isn_type, type, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001298 else
1299 isn = generate_instr_drop(cctx, isn_type, 1);
1300 if (isn == NULL)
1301 return FAIL;
1302
1303 // This requires three arguments, which doesn't fit in an instruction, thus
1304 // we need to allocate a struct for this.
1305 sref = ALLOC_ONE(scriptref_T);
1306 if (sref == NULL)
1307 return FAIL;
1308 isn->isn_arg.script.scriptref = sref;
1309 sref->sref_sid = sid;
1310 sref->sref_idx = idx;
1311 sref->sref_seq = si->sn_script_seq;
1312 sref->sref_type = type;
1313 return OK;
1314}
1315
1316/*
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001317 * Generate an ISN_NEWLIST instruction for "count" items.
1318 * "use_null" is TRUE for null_list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001319 */
1320 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001321generate_NEWLIST(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001322{
1323 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001324 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001325 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001326 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001327
1328 RETURN_OK_IF_SKIP(cctx);
1329 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1330 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001331 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001332
Bram Moolenaar078a4612022-01-04 15:17:03 +00001333 // Get the member type and the declared member type from all the items on
1334 // the stack.
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001335 member_type = get_member_type_from_stack(count, 1, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001336 type = get_list_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001337 decl_type = get_list_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001338
1339 // drop the value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001340 cctx->ctx_type_stack.ga_len -= count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001341
1342 // add the list type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001343 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001344}
1345
1346/*
1347 * Generate an ISN_NEWDICT instruction.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001348 * "use_null" is TRUE for null_dict.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001349 */
1350 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001351generate_NEWDICT(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001352{
1353 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001354 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001355 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001356 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001357
1358 RETURN_OK_IF_SKIP(cctx);
1359 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1360 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001361 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001362
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001363 member_type = get_member_type_from_stack(count, 2, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001364 type = get_dict_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001365 decl_type = get_dict_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001366
1367 // drop the key and value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001368 cctx->ctx_type_stack.ga_len -= 2 * count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001369
1370 // add the dict type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001371 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001372}
1373
1374/*
1375 * Generate an ISN_FUNCREF instruction.
Bram Moolenaar313e4722023-02-08 20:55:27 +00001376 * For "obj.Method" "cl" is the class of the object (can be an interface or a
1377 * base class) and "fi" the index of the method on that class.
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001378 * "isn_idx" is set to the index of the instruction, so that fr_dfunc_idx can
1379 * be set later. The index is used instead of a pointer to the instruction
1380 * because the instruction memory can be reallocated.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001381 */
1382 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001383generate_FUNCREF(
1384 cctx_T *cctx,
1385 ufunc_T *ufunc,
Bram Moolenaar313e4722023-02-08 20:55:27 +00001386 class_T *cl,
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001387 int object_method,
Bram Moolenaar313e4722023-02-08 20:55:27 +00001388 int fi,
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001389 int *isn_idx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001390{
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001391 isn_T *isn;
1392 type_T *type;
1393 funcref_extra_T *extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001394 loopvarinfo_T loopinfo;
1395 int has_vars;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001396
1397 RETURN_OK_IF_SKIP(cctx);
1398 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1399 return FAIL;
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001400 if (isn_idx != NULL)
1401 // save the index of the new instruction
1402 *isn_idx = cctx->ctx_instr.ga_len - 1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001403
Bram Moolenaarcc341812022-09-19 15:54:34 +01001404 has_vars = get_loop_var_info(cctx, &loopinfo);
Bram Moolenaar313e4722023-02-08 20:55:27 +00001405 if (ufunc->uf_def_status == UF_NOT_COMPILED || has_vars || cl != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001406 {
1407 extra = ALLOC_CLEAR_ONE(funcref_extra_T);
1408 if (extra == NULL)
1409 return FAIL;
1410 isn->isn_arg.funcref.fr_extra = extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001411 extra->fre_loopvar_info = loopinfo;
Bram Moolenaar313e4722023-02-08 20:55:27 +00001412 if (cl != NULL)
1413 {
1414 extra->fre_class = cl;
1415 ++cl->class_refcount;
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001416 extra->fre_object_method = object_method;
Bram Moolenaar313e4722023-02-08 20:55:27 +00001417 extra->fre_method_idx = fi;
1418 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001419 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00001420 if (ufunc->uf_def_status == UF_NOT_COMPILED || cl != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001421 extra->fre_func_name = vim_strsave(ufunc->uf_name);
Bram Moolenaar313e4722023-02-08 20:55:27 +00001422 if (ufunc->uf_def_status != UF_NOT_COMPILED && cl == NULL)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001423 {
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001424 if (isn_idx == NULL && ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001425 // compile the function now, we need the uf_dfunc_idx value
1426 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001427 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001428 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01001429
1430 // Reserve an extra variable to keep track of the number of closures
1431 // created.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001432 cctx->ctx_has_closure = 1;
1433
1434 // If the referenced function is a closure, it may use items further up in
Bram Moolenaar139575d2022-03-15 19:29:30 +00001435 // the nested context, including this one. But not a function defined at
1436 // the script level.
1437 if ((ufunc->uf_flags & FC_CLOSURE)
1438 && func_name_refcount(cctx->ctx_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001439 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1440
Bram Moolenaar078a4612022-01-04 15:17:03 +00001441 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1442 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001443}
1444
1445/*
1446 * Generate an ISN_NEWFUNC instruction.
1447 * "lambda_name" and "func_name" must be in allocated memory and will be
1448 * consumed.
1449 */
1450 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001451generate_NEWFUNC(
1452 cctx_T *cctx,
1453 char_u *lambda_name,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001454 char_u *func_name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001455{
1456 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001457 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001458
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001459 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001460 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001461 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1462 ret = FAIL;
1463 else
1464 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001465 newfuncarg_T *arg = ALLOC_CLEAR_ONE(newfuncarg_T);
1466
1467 if (arg == NULL)
1468 ret = FAIL;
1469 else
1470 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01001471 // Reserve an extra variable to keep track of the number of
1472 // closures created.
1473 cctx->ctx_has_closure = 1;
1474
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001475 isn->isn_arg.newfunc.nf_arg = arg;
1476 arg->nfa_lambda = lambda_name;
1477 arg->nfa_global = func_name;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001478 (void)get_loop_var_info(cctx, &arg->nfa_loopvar_info);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001479 return OK;
1480 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001481 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001482 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001483 vim_free(lambda_name);
1484 vim_free(func_name);
1485 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001486}
1487
1488/*
1489 * Generate an ISN_DEF instruction: list functions
1490 */
1491 int
1492generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1493{
1494 isn_T *isn;
1495
1496 RETURN_OK_IF_SKIP(cctx);
1497 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1498 return FAIL;
1499 if (len > 0)
1500 {
1501 isn->isn_arg.string = vim_strnsave(name, len);
1502 if (isn->isn_arg.string == NULL)
1503 return FAIL;
1504 }
1505 return OK;
1506}
1507
1508/*
1509 * Generate an ISN_JUMP instruction.
1510 */
1511 int
1512generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1513{
1514 isn_T *isn;
1515 garray_T *stack = &cctx->ctx_type_stack;
1516
1517 RETURN_OK_IF_SKIP(cctx);
1518 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1519 return FAIL;
1520 isn->isn_arg.jump.jump_when = when;
1521 isn->isn_arg.jump.jump_where = where;
1522
1523 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1524 --stack->ga_len;
1525
1526 return OK;
1527}
1528
1529/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001530 * Generate an ISN_WHILE instruction. Similar to ISN_JUMP for :while
1531 */
1532 int
1533generate_WHILE(cctx_T *cctx, int funcref_idx)
1534{
1535 isn_T *isn;
1536 garray_T *stack = &cctx->ctx_type_stack;
1537
1538 RETURN_OK_IF_SKIP(cctx);
1539 if ((isn = generate_instr(cctx, ISN_WHILE)) == NULL)
1540 return FAIL;
1541 isn->isn_arg.whileloop.while_funcref_idx = funcref_idx;
1542 isn->isn_arg.whileloop.while_end = 0; // filled in later
1543
1544 if (stack->ga_len > 0)
1545 --stack->ga_len;
1546
1547 return OK;
1548}
1549
1550/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001551 * Generate an ISN_JUMP_IF_ARG_SET or ISN_JUMP_IF_ARG_NOT_SET instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001552 */
1553 int
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001554generate_JUMP_IF_ARG(cctx_T *cctx, isntype_T isn_type, int arg_off)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001555{
1556 isn_T *isn;
1557
1558 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001559 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001560 return FAIL;
1561 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1562 // jump_where is set later
1563 return OK;
1564}
1565
1566 int
1567generate_FOR(cctx_T *cctx, int loop_idx)
1568{
1569 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001570
1571 RETURN_OK_IF_SKIP(cctx);
1572 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1573 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001574 isn->isn_arg.forloop.for_loop_idx = loop_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001575
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001576 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001577 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001578}
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001579
1580 int
Bram Moolenaarcc341812022-09-19 15:54:34 +01001581generate_ENDLOOP(cctx_T *cctx, loop_info_T *loop_info)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001582{
1583 isn_T *isn;
1584
1585 RETURN_OK_IF_SKIP(cctx);
1586 if ((isn = generate_instr(cctx, ISN_ENDLOOP)) == NULL)
1587 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001588 isn->isn_arg.endloop.end_depth = loop_info->li_depth;
1589 isn->isn_arg.endloop.end_funcref_idx = loop_info->li_funcref_idx;
1590 isn->isn_arg.endloop.end_var_idx = loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001591 isn->isn_arg.endloop.end_var_count =
Bram Moolenaarcc341812022-09-19 15:54:34 +01001592 cctx->ctx_locals.ga_len - loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001593 return OK;
1594}
1595
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001596/*
1597 * Generate an ISN_TRYCONT instruction.
1598 */
1599 int
1600generate_TRYCONT(cctx_T *cctx, int levels, int where)
1601{
1602 isn_T *isn;
1603
1604 RETURN_OK_IF_SKIP(cctx);
1605 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1606 return FAIL;
1607 isn->isn_arg.trycont.tct_levels = levels;
1608 isn->isn_arg.trycont.tct_where = where;
1609
1610 return OK;
1611}
1612
Bram Moolenaar16900322022-09-08 19:51:45 +01001613/*
1614 * Check "argount" arguments and their types on the type stack.
1615 * Give an error and return FAIL if something is wrong.
1616 * When "method_call" is NULL no code is generated.
1617 */
1618 int
1619check_internal_func_args(
1620 cctx_T *cctx,
1621 int func_idx,
1622 int argcount,
1623 int method_call,
1624 type2_T **argtypes,
1625 type2_T *shuffled_argtypes)
1626{
1627 garray_T *stack = &cctx->ctx_type_stack;
1628 int argoff = check_internal_func(func_idx, argcount);
1629
1630 if (argoff < 0)
1631 return FAIL;
1632
1633 if (method_call && argoff > 1)
1634 {
Bram Moolenaarb7f22702023-04-27 16:24:07 +01001635 if (argcount < argoff)
1636 {
1637 semsg(_(e_not_enough_arguments_for_function_str),
1638 internal_func_name(func_idx));
1639 return FAIL;
1640 }
Bram Moolenaar16900322022-09-08 19:51:45 +01001641
Bram Moolenaarb7f22702023-04-27 16:24:07 +01001642 isn_T *isn = generate_instr(cctx, ISN_SHUFFLE);
Bram Moolenaar16900322022-09-08 19:51:45 +01001643 if (isn == NULL)
1644 return FAIL;
1645 isn->isn_arg.shuffle.shfl_item = argcount;
1646 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1647 }
1648
1649 if (argcount > 0)
1650 {
1651 type2_T *typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
1652
1653 // Check the types of the arguments.
1654 if (method_call && argoff > 1)
1655 {
1656 int i;
1657
1658 for (i = 0; i < argcount; ++i)
1659 shuffled_argtypes[i] = (i < argoff - 1)
1660 ? typep[i + 1]
1661 : (i == argoff - 1) ? typep[0] : typep[i];
1662 *argtypes = shuffled_argtypes;
1663 }
1664 else
1665 {
1666 int i;
1667
1668 for (i = 0; i < argcount; ++i)
1669 shuffled_argtypes[i] = typep[i];
1670 *argtypes = shuffled_argtypes;
1671 }
1672 if (internal_func_check_arg_types(*argtypes, func_idx, argcount,
1673 cctx) == FAIL)
1674 return FAIL;
1675 }
1676 return OK;
1677}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001678
1679/*
1680 * Generate an ISN_BCALL instruction.
1681 * "method_call" is TRUE for "value->method()"
1682 * Return FAIL if the number of arguments is wrong.
1683 */
1684 int
1685generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1686{
1687 isn_T *isn;
1688 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001689 type2_T *argtypes = NULL;
1690 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1691 type2_T *maptype = NULL;
1692 type_T *type;
Bram Moolenaar81330182022-02-01 12:11:58 +00001693 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001694
1695 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar16900322022-09-08 19:51:45 +01001696
1697 if (check_internal_func_args(cctx, func_idx, argcount, method_call,
1698 &argtypes, shuffled_argtypes) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001699 return FAIL;
1700
Bram Moolenaar16900322022-09-08 19:51:45 +01001701 if (internal_func_is_map(func_idx))
1702 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001703
1704 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1705 return FAIL;
1706 isn->isn_arg.bfunc.cbf_idx = func_idx;
1707 isn->isn_arg.bfunc.cbf_argcount = argcount;
1708
1709 // Drop the argument types and push the return type.
1710 stack->ga_len -= argcount;
Bram Moolenaar32517c42023-01-15 18:17:12 +00001711 type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type,
1712 cctx->ctx_type_list);
Bram Moolenaar81330182022-02-01 12:11:58 +00001713 if (push_type_stack2(cctx, type, decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001714 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001715
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001716 if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
1717 && maptype[0].type_decl->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001718 // Check that map() didn't change the item types.
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001719 generate_TYPECHECK(cctx, maptype[0].type_decl, FALSE, -1, FALSE, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001720
1721 return OK;
1722}
1723
1724/*
1725 * Generate an ISN_LISTAPPEND instruction. Works like add().
1726 * Argument count is already checked.
1727 */
1728 int
1729generate_LISTAPPEND(cctx_T *cctx)
1730{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001731 type_T *list_type;
1732 type_T *item_type;
1733 type_T *expected;
1734
1735 // Caller already checked that list_type is a list.
Bram Moolenaar81330182022-02-01 12:11:58 +00001736 // For checking the item type we use the declared type of the list and the
1737 // current type of the added item, adding a string to [1, 2] is OK.
1738 list_type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaarfa103972022-09-29 19:14:42 +01001739 if (arg_type_modifiable(list_type, 1) == FAIL)
1740 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001741 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001742 expected = list_type->tt_member;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001743 if (need_type(item_type, expected, FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001744 return FAIL;
1745
1746 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1747 return FAIL;
1748
Bram Moolenaar078a4612022-01-04 15:17:03 +00001749 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001750 return OK;
1751}
1752
1753/*
1754 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1755 * Argument count is already checked.
1756 */
1757 int
1758generate_BLOBAPPEND(cctx_T *cctx)
1759{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001760 type_T *item_type;
1761
Bram Moolenaarfa103972022-09-29 19:14:42 +01001762 // Caller already checked that blob_type is a blob, check it is modifiable.
1763 if (arg_type_modifiable(get_decl_type_on_stack(cctx, 1), 1) == FAIL)
1764 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001765 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001766 if (need_type(item_type, &t_number, FALSE,
1767 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001768 return FAIL;
1769
1770 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1771 return FAIL;
1772
Bram Moolenaar078a4612022-01-04 15:17:03 +00001773 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001774 return OK;
1775}
1776
1777/*
Bram Moolenaard0200c82023-01-28 15:19:40 +00001778 * Generate an ISN_DCALL, ISN_UCALL or ISN_METHODCALL instruction.
1779 * When calling a method on an object, of which we know the interface only,
1780 * then "cl" is the interface and "mi" the method index on the interface.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001781 * Return FAIL if the number of arguments is wrong.
1782 */
1783 int
Bram Moolenaard0200c82023-01-28 15:19:40 +00001784generate_CALL(
1785 cctx_T *cctx,
1786 ufunc_T *ufunc,
1787 class_T *cl,
1788 int mi,
1789 int pushed_argcount)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001790{
1791 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001792 int regular_args = ufunc->uf_args.ga_len;
1793 int argcount = pushed_argcount;
1794
1795 RETURN_OK_IF_SKIP(cctx);
1796 if (argcount > regular_args && !has_varargs(ufunc))
1797 {
1798 semsg(_(e_too_many_arguments_for_function_str),
1799 printable_func_name(ufunc));
1800 return FAIL;
1801 }
1802 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1803 {
1804 semsg(_(e_not_enough_arguments_for_function_str),
1805 printable_func_name(ufunc));
1806 return FAIL;
1807 }
1808
1809 if (ufunc->uf_def_status != UF_NOT_COMPILED
1810 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1811 {
1812 int i;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001813 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001814
1815 for (i = 0; i < argcount; ++i)
1816 {
1817 type_T *expected;
1818 type_T *actual;
1819
Bram Moolenaar078a4612022-01-04 15:17:03 +00001820 actual = get_type_on_stack(cctx, argcount - i - 1);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001821 if (actual->tt_type == VAR_SPECIAL
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001822 && i >= regular_args - ufunc->uf_def_args.ga_len)
1823 {
1824 // assume v:none used for default argument value
1825 continue;
1826 }
1827 if (i < regular_args)
1828 {
1829 if (ufunc->uf_arg_types == NULL)
1830 continue;
1831 expected = ufunc->uf_arg_types[i];
1832 }
1833 else if (ufunc->uf_va_type == NULL
1834 || ufunc->uf_va_type == &t_list_any)
1835 // possibly a lambda or "...: any"
1836 expected = &t_any;
1837 else
1838 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001839 if (need_type(actual, expected, FALSE,
1840 -argcount + i, i + 1, cctx, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001841 {
1842 arg_type_mismatch(expected, actual, i + 1);
1843 return FAIL;
1844 }
1845 }
Bram Moolenaar139575d2022-03-15 19:29:30 +00001846 compile_type = get_compile_type(ufunc);
1847 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001848 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaar139575d2022-03-15 19:29:30 +00001849 compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001850 return FAIL;
1851 }
1852 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1853 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001854 emsg_funcname(e_call_to_function_that_failed_to_compile_str,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001855 ufunc->uf_name);
1856 return FAIL;
1857 }
1858
Bram Moolenaard0200c82023-01-28 15:19:40 +00001859 if ((isn = generate_instr(cctx, cl != NULL ? ISN_METHODCALL
1860 : ufunc->uf_def_status != UF_NOT_COMPILED
1861 ? ISN_DCALL : ISN_UCALL)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001862 return FAIL;
Bram Moolenaar836137d2023-01-29 14:11:24 +00001863 if (cl != NULL /* isn->isn_type == ISN_METHODCALL */)
Bram Moolenaard0200c82023-01-28 15:19:40 +00001864 {
1865 isn->isn_arg.mfunc = ALLOC_ONE(cmfunc_T);
1866 if (isn->isn_arg.mfunc == NULL)
1867 return FAIL;
1868 isn->isn_arg.mfunc->cmf_itf = cl;
1869 ++cl->class_refcount;
1870 isn->isn_arg.mfunc->cmf_idx = mi;
1871 isn->isn_arg.mfunc->cmf_argcount = argcount;
1872 }
1873 else if (isn->isn_type == ISN_DCALL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001874 {
1875 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1876 isn->isn_arg.dfunc.cdf_argcount = argcount;
1877 }
1878 else
1879 {
1880 // A user function may be deleted and redefined later, can't use the
1881 // ufunc pointer, need to look it up again at runtime.
1882 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1883 isn->isn_arg.ufunc.cuf_argcount = argcount;
1884 }
1885
Bram Moolenaar078a4612022-01-04 15:17:03 +00001886 // drop the argument types
1887 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001888
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02001889 // For an object or class method call, drop the object/class type.
Yegappan Lakshmanan639751d2023-08-27 19:23:37 +02001890 if (ufunc->uf_class != NULL)
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02001891 {
1892 // When a class method is called without the class name prefix, then
1893 // the type will not be in the stack.
1894 type_T *stype = get_type_on_stack(cctx, 0);
1895 if (stype->tt_type == VAR_CLASS || stype->tt_type == VAR_OBJECT)
1896 cctx->ctx_type_stack.ga_len--;
1897 }
Yegappan Lakshmanan639751d2023-08-27 19:23:37 +02001898
Bram Moolenaar078a4612022-01-04 15:17:03 +00001899 // add return type
1900 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001901}
1902
1903/*
1904 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1905 */
1906 int
1907generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1908{
1909 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001910
1911 RETURN_OK_IF_SKIP(cctx);
1912 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1913 return FAIL;
1914 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1915 isn->isn_arg.ufunc.cuf_argcount = argcount;
1916
Bram Moolenaar078a4612022-01-04 15:17:03 +00001917 // drop the argument types
1918 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001919
Bram Moolenaar078a4612022-01-04 15:17:03 +00001920 // add return value
1921 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001922}
1923
1924/*
Bram Moolenaar16900322022-09-08 19:51:45 +01001925 * Check the arguments of function "type" against the types on the stack.
1926 * Returns OK or FAIL;
1927 */
1928 int
1929check_func_args_from_type(
1930 cctx_T *cctx,
1931 type_T *type,
1932 int argcount,
1933 int at_top,
1934 char_u *name)
1935{
1936 if (type->tt_argcount != -1)
1937 {
1938 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1939
1940 if (argcount < type->tt_min_argcount - varargs)
1941 {
1942 emsg_funcname(e_not_enough_arguments_for_function_str, name);
1943 return FAIL;
1944 }
1945 if (!varargs && argcount > type->tt_argcount)
1946 {
1947 emsg_funcname(e_too_many_arguments_for_function_str, name);
1948 return FAIL;
1949 }
1950 if (type->tt_args != NULL)
1951 {
1952 int i;
1953
1954 for (i = 0; i < argcount; ++i)
1955 {
1956 int offset = -argcount + i - (at_top ? 0 : 1);
1957 type_T *actual = get_type_on_stack(cctx, -1 - offset);
1958 type_T *expected;
1959
1960 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaar36818a92023-01-03 12:33:26 +00001961 {
1962 expected = type->tt_args[type->tt_argcount - 1];
1963 if (expected != NULL && expected->tt_type == VAR_LIST)
1964 expected = expected->tt_member;
1965 if (expected == NULL)
1966 expected = &t_any;
1967 }
Bram Moolenaar16900322022-09-08 19:51:45 +01001968 else if (i >= type->tt_min_argcount
1969 && actual->tt_type == VAR_SPECIAL)
1970 expected = &t_any;
1971 else
1972 expected = type->tt_args[i];
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001973 if (need_type(actual, expected, FALSE,
1974 offset, i + 1, cctx, TRUE, FALSE) == FAIL)
Bram Moolenaar16900322022-09-08 19:51:45 +01001975 {
1976 arg_type_mismatch(expected, actual, i + 1);
1977 return FAIL;
1978 }
1979 }
1980 }
1981 }
1982
1983 return OK;
1984}
1985/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001986 * Generate an ISN_PCALL instruction.
1987 * "type" is the type of the FuncRef.
1988 */
1989 int
1990generate_PCALL(
1991 cctx_T *cctx,
1992 int argcount,
1993 char_u *name,
1994 type_T *type,
1995 int at_top)
1996{
1997 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001998 type_T *ret_type;
1999
2000 RETURN_OK_IF_SKIP(cctx);
2001
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002002 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002003 ret_type = &t_any;
2004 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
2005 {
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002006 if (check_func_args_from_type(cctx, type, argcount, at_top, name)
2007 == FAIL)
Bram Moolenaar16900322022-09-08 19:51:45 +01002008 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002009
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002010 ret_type = type->tt_member;
2011 if (ret_type == &t_unknown)
2012 // return type not known yet, use a runtime check
2013 ret_type = &t_any;
2014 }
2015 else
2016 {
2017 semsg(_(e_not_callable_type_str), name);
2018 return FAIL;
2019 }
2020
2021 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2022 return FAIL;
2023 isn->isn_arg.pfunc.cpf_top = at_top;
2024 isn->isn_arg.pfunc.cpf_argcount = argcount;
2025
Bram Moolenaar078a4612022-01-04 15:17:03 +00002026 // drop the arguments and the funcref/partial
2027 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002028
Bram Moolenaar078a4612022-01-04 15:17:03 +00002029 // push the return value
2030 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002031
2032 // If partial is above the arguments it must be cleared and replaced with
2033 // the return value.
2034 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2035 return FAIL;
2036
2037 return OK;
2038}
2039
2040/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002041 * Generate an ISN_DEFER instruction.
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00002042 * "obj_method" is one for "obj.Method()", zero otherwise.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002043 */
2044 int
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00002045generate_DEFER(cctx_T *cctx, int var_idx, int obj_method, int argcount)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002046{
2047 isn_T *isn;
2048
2049 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00002050 if ((isn = generate_instr_drop(cctx,
2051 obj_method == 0 ? ISN_DEFER : ISN_DEFEROBJ,
2052 argcount + 1)) == NULL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002053 return FAIL;
2054 isn->isn_arg.defer.defer_var_idx = var_idx;
2055 isn->isn_arg.defer.defer_argcount = argcount;
2056 return OK;
2057}
2058
2059/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002060 * Generate an ISN_STRINGMEMBER instruction.
2061 */
2062 int
2063generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
2064{
2065 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002066 type_T *type;
2067
2068 RETURN_OK_IF_SKIP(cctx);
2069 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
2070 return FAIL;
2071 isn->isn_arg.string = vim_strnsave(name, len);
2072
2073 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00002074 type = get_type_on_stack(cctx, 0);
Bram Moolenaar0089ce22022-10-08 14:39:36 +01002075 if (type->tt_type != VAR_DICT
2076 && type->tt_type != VAR_ANY && type->tt_type != VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002077 {
2078 char *tofree;
2079
2080 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
2081 name, type_name(type, &tofree));
2082 vim_free(tofree);
2083 return FAIL;
2084 }
2085 // change dict type to dict member type
2086 if (type->tt_type == VAR_DICT)
2087 {
Bram Moolenaar0089ce22022-10-08 14:39:36 +01002088 type_T *ntype = type->tt_member->tt_type == VAR_UNKNOWN
Bram Moolenaar078a4612022-01-04 15:17:03 +00002089 ? &t_any : type->tt_member;
2090 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002091 }
2092
2093 return OK;
2094}
2095
2096/*
2097 * Generate an ISN_ECHO instruction.
2098 */
2099 int
2100generate_ECHO(cctx_T *cctx, int with_white, int count)
2101{
2102 isn_T *isn;
2103
2104 RETURN_OK_IF_SKIP(cctx);
2105 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2106 return FAIL;
2107 isn->isn_arg.echo.echo_with_white = with_white;
2108 isn->isn_arg.echo.echo_count = count;
2109
2110 return OK;
2111}
2112
2113/*
2114 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
2115 */
2116 int
2117generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
2118{
2119 isn_T *isn;
2120
Bram Moolenaar2eae3d22022-10-07 15:09:27 +01002121 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002122 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
2123 return FAIL;
2124 isn->isn_arg.number = count;
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002125 return OK;
2126}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002127
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002128/*
2129 * Generate an ISN_ECHOWINDOW instruction
2130 */
2131 int
2132generate_ECHOWINDOW(cctx_T *cctx, int count, long time)
2133{
2134 isn_T *isn;
2135
2136 if ((isn = generate_instr_drop(cctx, ISN_ECHOWINDOW, count)) == NULL)
2137 return FAIL;
2138 isn->isn_arg.echowin.ewin_count = count;
2139 isn->isn_arg.echowin.ewin_time = time;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002140 return OK;
2141}
2142
2143/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002144 * Generate an ISN_SOURCE instruction.
2145 */
2146 int
2147generate_SOURCE(cctx_T *cctx, int sid)
2148{
2149 isn_T *isn;
2150
2151 if ((isn = generate_instr(cctx, ISN_SOURCE)) == NULL)
2152 return FAIL;
2153 isn->isn_arg.number = sid;
2154
2155 return OK;
2156}
2157
2158/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002159 * Generate an ISN_PUT instruction.
2160 */
2161 int
2162generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2163{
2164 isn_T *isn;
2165
2166 RETURN_OK_IF_SKIP(cctx);
2167 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2168 return FAIL;
2169 isn->isn_arg.put.put_regname = regname;
2170 isn->isn_arg.put.put_lnum = lnum;
2171 return OK;
2172}
2173
2174/*
Ernie Rael64885642023-10-04 20:16:22 +02002175 * Generate a LOCKUNLOCK instruction.The root item, where the indexing starts
2176 * to find the variable, is on the stack. The instr takes
2177 * - the string to parse, "root.b[idx1][idx2].d.val", to find the variable
2178 * - the class, if any, in which the string executes.
2179 * - if the root item is a function argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002180 * A copy is made of "line".
2181 */
2182 int
Ernie Raelee865f32023-09-29 19:53:55 +02002183generate_LOCKUNLOCK(cctx_T *cctx, char_u *line, int is_arg)
2184{
2185 isn_T *isn;
2186
2187 RETURN_OK_IF_SKIP(cctx);
2188 if ((isn = generate_instr(cctx, ISN_LOCKUNLOCK)) == NULL)
2189 return FAIL;
Ernie Rael64885642023-10-04 20:16:22 +02002190 class_T *cl = cctx->ctx_ufunc != NULL ? cctx->ctx_ufunc->uf_class : NULL;
2191 isn->isn_arg.lockunlock.lu_string = vim_strsave(line);
2192 isn->isn_arg.lockunlock.lu_cl_exec = cl;
2193 if (cl != NULL)
2194 ++cl->class_refcount;
2195 isn->isn_arg.lockunlock.lu_is_arg = is_arg;
Ernie Raelee865f32023-09-29 19:53:55 +02002196 return OK;
2197}
2198
2199/*
2200 * Generate an EXEC instruction that takes a string argument.
2201 * A copy is made of "line".
2202 */
2203 int
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002204generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
2205{
2206 isn_T *isn;
2207
2208 RETURN_OK_IF_SKIP(cctx);
2209 if ((isn = generate_instr(cctx, isntype)) == NULL)
2210 return FAIL;
2211 isn->isn_arg.string = vim_strsave(line);
2212 return OK;
2213}
2214
2215/*
2216 * Generate an EXEC instruction that takes a string argument.
2217 * "str" must be allocated, it is consumed.
2218 */
2219 int
2220generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
2221{
2222 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002223 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002224
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002225 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002226 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002227 if ((isn = generate_instr(cctx, isntype)) == NULL)
2228 ret = FAIL;
2229 else
2230 {
2231 isn->isn_arg.string = str;
2232 return OK;
2233 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002234 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002235 vim_free(str);
2236 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002237}
2238
2239 int
2240generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2241{
2242 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002243
2244 RETURN_OK_IF_SKIP(cctx);
2245 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2246 return FAIL;
2247 isn->isn_arg.string = vim_strsave(line);
2248
Bram Moolenaar078a4612022-01-04 15:17:03 +00002249 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002250}
2251
2252 int
2253generate_EXECCONCAT(cctx_T *cctx, int count)
2254{
2255 isn_T *isn;
2256
2257 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2258 return FAIL;
2259 isn->isn_arg.number = count;
2260 return OK;
2261}
2262
2263/*
2264 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2265 */
2266 int
2267generate_RANGE(cctx_T *cctx, char_u *range)
2268{
2269 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002270
2271 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2272 return FAIL;
2273 isn->isn_arg.string = range;
2274
Bram Moolenaar078a4612022-01-04 15:17:03 +00002275 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002276}
2277
2278 int
2279generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2280{
2281 isn_T *isn;
2282
2283 RETURN_OK_IF_SKIP(cctx);
2284 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2285 return FAIL;
2286 isn->isn_arg.unpack.unp_count = var_count;
2287 isn->isn_arg.unpack.unp_semicolon = semicolon;
2288 return OK;
2289}
2290
2291/*
2292 * Generate an instruction for any command modifiers.
2293 */
2294 int
2295generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
2296{
2297 isn_T *isn;
2298
2299 if (has_cmdmod(cmod, FALSE))
2300 {
2301 cctx->ctx_has_cmdmod = TRUE;
2302
2303 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
2304 return FAIL;
2305 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2306 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2307 return FAIL;
2308 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
2309 // filter program now belongs to the instruction
2310 cmod->cmod_filter_regmatch.regprog = NULL;
2311 }
2312
2313 return OK;
2314}
2315
2316 int
2317generate_undo_cmdmods(cctx_T *cctx)
2318{
2319 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2320 return FAIL;
2321 cctx->ctx_has_cmdmod = FALSE;
2322 return OK;
2323}
2324
2325/*
2326 * Generate a STORE instruction for "dest", not being "dest_local".
Bram Moolenaard505d172022-12-18 21:42:55 +00002327 * "lhs" might be NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002328 * Return FAIL when out of memory.
2329 */
2330 int
2331generate_store_var(
2332 cctx_T *cctx,
2333 assign_dest_T dest,
2334 int opt_flags,
2335 int vimvaridx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002336 type_T *type,
Bram Moolenaard505d172022-12-18 21:42:55 +00002337 char_u *name,
2338 lhs_T *lhs)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002339{
2340 switch (dest)
2341 {
2342 case dest_option:
2343 return generate_STOREOPT(cctx, ISN_STOREOPT,
2344 skip_option_env_lead(name), opt_flags);
2345 case dest_func_option:
2346 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
2347 skip_option_env_lead(name), opt_flags);
2348 case dest_global:
2349 // include g: with the name, easier to execute that way
2350 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
2351 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
2352 case dest_buffer:
2353 // include b: with the name, easier to execute that way
2354 return generate_STORE(cctx, ISN_STOREB, 0, name);
2355 case dest_window:
2356 // include w: with the name, easier to execute that way
2357 return generate_STORE(cctx, ISN_STOREW, 0, name);
2358 case dest_tab:
2359 // include t: with the name, easier to execute that way
2360 return generate_STORE(cctx, ISN_STORET, 0, name);
2361 case dest_env:
2362 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
2363 case dest_reg:
2364 return generate_STORE(cctx, ISN_STOREREG,
2365 name[1] == '@' ? '"' : name[1], NULL);
2366 case dest_vimvar:
2367 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
2368 case dest_script:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002369 {
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002370 int scriptvar_idx = lhs->lhs_scriptvar_idx;
2371 int scriptvar_sid = lhs->lhs_scriptvar_sid;
2372 if (scriptvar_idx < 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002373 {
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002374 isntype_T isn_type = ISN_STORES;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002375
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002376 if (SCRIPT_ID_VALID(scriptvar_sid)
2377 && SCRIPT_ITEM(scriptvar_sid)->sn_import_autoload
2378 && SCRIPT_ITEM(scriptvar_sid)->sn_autoload_prefix
2379 == NULL)
2380 {
2381 // "import autoload './dir/script.vim'" - load script
2382 // first
2383 if (generate_SOURCE(cctx, scriptvar_sid) == FAIL)
2384 return FAIL;
2385 isn_type = ISN_STOREEXPORT;
2386 }
2387
2388 // "s:" may be included in the name.
2389 return generate_OLDSCRIPT(cctx, isn_type, name,
2390 scriptvar_sid, type);
2391 }
2392 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002393 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002394 }
Bram Moolenaard505d172022-12-18 21:42:55 +00002395 case dest_class_member:
2396 return generate_CLASSMEMBER(cctx, FALSE,
2397 lhs->lhs_class, lhs->lhs_classmember_idx);
2398
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002399 case dest_local:
2400 case dest_expr:
2401 // cannot happen
2402 break;
2403 }
2404 return FAIL;
2405}
2406
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002407/*
2408 * Return TRUE when inside a "for" or "while" loop.
2409 */
2410 int
2411inside_loop_scope(cctx_T *cctx)
2412{
2413 scope_T *scope = cctx->ctx_scope;
2414
2415 for (;;)
2416 {
2417 if (scope == NULL)
2418 break;
2419 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
2420 return TRUE;
2421 scope = scope->se_outer;
2422 }
2423 return FALSE;
2424}
2425
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002426 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002427generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002428{
2429 if (lhs->lhs_dest != dest_local)
2430 return generate_store_var(cctx, lhs->lhs_dest,
2431 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
Bram Moolenaard505d172022-12-18 21:42:55 +00002432 lhs->lhs_type, lhs->lhs_name, lhs);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002433
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002434 if (lhs->lhs_lvar == NULL)
2435 return OK;
2436
2437 garray_T *instr = &cctx->ctx_instr;
2438 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2439
2440 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
2441 // ISN_STORENR.
2442 // And "var = 0" does not need any instruction.
2443 if (lhs->lhs_lvar->lv_from_outer == 0
2444 && instr->ga_len == instr_count + 1
2445 && isn->isn_type == ISN_PUSHNR)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002446 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002447 varnumber_T val = isn->isn_arg.number;
2448 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002449
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002450 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002451 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002452 // zero is the default value, no need to do anything
2453 --instr->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002454 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002455 else
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002456 {
2457 isn->isn_type = ISN_STORENR;
2458 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
2459 isn->isn_arg.storenr.stnr_val = val;
2460 }
2461 if (stack->ga_len > 0)
2462 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002463 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002464 else if (lhs->lhs_lvar->lv_from_outer > 0)
2465 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
2466 lhs->lhs_lvar->lv_from_outer, lhs->lhs_lvar->lv_loop_idx);
2467 else
2468 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002469 return OK;
2470}
2471
2472#if defined(FEAT_PROFILE) || defined(PROTO)
2473 void
2474may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2475{
2476 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
2477 generate_instr(cctx, ISN_PROF_END);
2478}
2479#endif
2480
2481
2482/*
2483 * Delete an instruction, free what it contains.
2484 */
2485 void
2486delete_instr(isn_T *isn)
2487{
2488 switch (isn->isn_type)
2489 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002490 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002491 case ISN_DEF:
2492 case ISN_EXEC:
2493 case ISN_EXECRANGE:
2494 case ISN_EXEC_SPLIT:
2495 case ISN_LEGACY_EVAL:
2496 case ISN_LOADAUTO:
2497 case ISN_LOADB:
2498 case ISN_LOADENV:
2499 case ISN_LOADG:
2500 case ISN_LOADOPT:
2501 case ISN_LOADT:
2502 case ISN_LOADW:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002503 case ISN_PUSHEXC:
2504 case ISN_PUSHFUNC:
2505 case ISN_PUSHS:
2506 case ISN_RANGE:
2507 case ISN_STOREAUTO:
2508 case ISN_STOREB:
2509 case ISN_STOREENV:
2510 case ISN_STOREG:
2511 case ISN_STORET:
2512 case ISN_STOREW:
2513 case ISN_STRINGMEMBER:
2514 vim_free(isn->isn_arg.string);
2515 break;
2516
Ernie Rael64885642023-10-04 20:16:22 +02002517 case ISN_LOCKUNLOCK:
2518 class_unref(isn->isn_arg.lockunlock.lu_cl_exec);
2519 vim_free(isn->isn_arg.lockunlock.lu_string);
2520 break;
2521
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002522 case ISN_SUBSTITUTE:
2523 {
2524 int idx;
2525 isn_T *list = isn->isn_arg.subs.subs_instr;
2526
2527 vim_free(isn->isn_arg.subs.subs_cmd);
2528 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2529 delete_instr(list + idx);
2530 vim_free(list);
2531 }
2532 break;
2533
2534 case ISN_INSTR:
2535 {
2536 int idx;
2537 isn_T *list = isn->isn_arg.instr;
2538
2539 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2540 delete_instr(list + idx);
2541 vim_free(list);
2542 }
2543 break;
2544
2545 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002546 case ISN_LOADEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002547 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002548 case ISN_STOREEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002549 vim_free(isn->isn_arg.loadstore.ls_name);
2550 break;
2551
2552 case ISN_UNLET:
2553 case ISN_UNLETENV:
2554 vim_free(isn->isn_arg.unlet.ul_name);
2555 break;
2556
2557 case ISN_STOREOPT:
2558 case ISN_STOREFUNCOPT:
2559 vim_free(isn->isn_arg.storeopt.so_name);
2560 break;
2561
2562 case ISN_PUSHBLOB: // push blob isn_arg.blob
2563 blob_unref(isn->isn_arg.blob);
2564 break;
2565
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002566 case ISN_PUSHCLASS:
Bram Moolenaar30a84472023-02-27 08:07:14 +00002567 class_unref(isn->isn_arg.classarg);
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002568 break;
2569
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002570 case ISN_UCALL:
2571 vim_free(isn->isn_arg.ufunc.cuf_name);
2572 break;
2573
2574 case ISN_FUNCREF:
2575 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002576 funcref_T *funcref = &isn->isn_arg.funcref;
2577 funcref_extra_T *extra = funcref->fr_extra;
2578
2579 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002580 {
2581 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002582 + funcref->fr_dfunc_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002583 ufunc_T *ufunc = dfunc->df_ufunc;
2584
2585 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2586 func_ptr_unref(ufunc);
2587 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002588 if (extra != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002589 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002590 char_u *name = extra->fre_func_name;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002591
2592 if (name != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002593 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002594 func_unref(name);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002595 vim_free(name);
2596 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00002597 if (extra->fre_class != NULL)
2598 class_unref(extra->fre_class);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002599 vim_free(extra);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002600 }
2601 }
2602 break;
2603
2604 case ISN_DCALL:
2605 {
2606 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002607 + isn->isn_arg.dfunc.cdf_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002608
2609 if (dfunc->df_ufunc != NULL
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002610 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002611 func_ptr_unref(dfunc->df_ufunc);
2612 }
2613 break;
2614
Bram Moolenaard0200c82023-01-28 15:19:40 +00002615 case ISN_METHODCALL:
2616 {
2617 cmfunc_T *mfunc = isn->isn_arg.mfunc;
2618 class_unref(mfunc->cmf_itf);
2619 vim_free(mfunc);
2620 }
2621 break;
2622
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002623 case ISN_NEWFUNC:
2624 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002625 newfuncarg_T *arg = isn->isn_arg.newfunc.nf_arg;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002626
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002627 if (arg != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002628 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002629 ufunc_T *ufunc = find_func_even_dead(
2630 arg->nfa_lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002631
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002632 if (ufunc != NULL)
2633 {
2634 unlink_def_function(ufunc);
2635 func_ptr_unref(ufunc);
2636 }
2637
2638 vim_free(arg->nfa_lambda);
2639 vim_free(arg->nfa_global);
2640 vim_free(arg);
2641 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002642 }
2643 break;
2644
2645 case ISN_CHECKTYPE:
2646 case ISN_SETTYPE:
2647 free_type(isn->isn_arg.type.ct_type);
2648 break;
2649
2650 case ISN_CMDMOD:
2651 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002652 ->cmod_filter_regmatch.regprog);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002653 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2654 break;
2655
2656 case ISN_LOADSCRIPT:
2657 case ISN_STORESCRIPT:
2658 vim_free(isn->isn_arg.script.scriptref);
2659 break;
2660
Bram Moolenaard505d172022-12-18 21:42:55 +00002661 case ISN_LOAD_CLASSMEMBER:
2662 case ISN_STORE_CLASSMEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00002663 case ISN_GET_ITF_MEMBER:
Bram Moolenaard505d172022-12-18 21:42:55 +00002664 class_unref(isn->isn_arg.classmember.cm_class);
2665 break;
2666
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002667 case ISN_STOREINDEX:
2668 class_unref(isn->isn_arg.storeindex.si_class);
2669 break;
2670
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002671 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002672 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002673 break;
2674
2675 case ISN_CEXPR_CORE:
2676 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2677 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2678 break;
2679
2680 case ISN_2BOOL:
2681 case ISN_2STRING:
2682 case ISN_2STRING_ANY:
2683 case ISN_ADDBLOB:
2684 case ISN_ADDLIST:
2685 case ISN_ANYINDEX:
2686 case ISN_ANYSLICE:
2687 case ISN_BCALL:
2688 case ISN_BLOBAPPEND:
2689 case ISN_BLOBINDEX:
2690 case ISN_BLOBSLICE:
2691 case ISN_CATCH:
2692 case ISN_CEXPR_AUCMD:
2693 case ISN_CHECKLEN:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002694 case ISN_CLEARDICT:
2695 case ISN_CMDMOD_REV:
2696 case ISN_COMPAREANY:
2697 case ISN_COMPAREBLOB:
2698 case ISN_COMPAREBOOL:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002699 case ISN_COMPARECLASS:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002700 case ISN_COMPAREDICT:
2701 case ISN_COMPAREFLOAT:
2702 case ISN_COMPAREFUNC:
2703 case ISN_COMPARELIST:
2704 case ISN_COMPARENR:
Bram Moolenaar7a222242022-03-01 19:23:24 +00002705 case ISN_COMPARENULL:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002706 case ISN_COMPAREOBJECT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002707 case ISN_COMPARESPECIAL:
2708 case ISN_COMPARESTRING:
2709 case ISN_CONCAT:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002710 case ISN_CONSTRUCT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002711 case ISN_COND2BOOL:
2712 case ISN_DEBUG:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002713 case ISN_DEFER:
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00002714 case ISN_DEFEROBJ:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002715 case ISN_DROP:
2716 case ISN_ECHO:
2717 case ISN_ECHOCONSOLE:
2718 case ISN_ECHOERR:
2719 case ISN_ECHOMSG:
Bram Moolenaar68a635a2022-09-01 17:26:17 +01002720 case ISN_ECHOWINDOW:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002721 case ISN_ENDLOOP:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002722 case ISN_ENDTRY:
2723 case ISN_EXECCONCAT:
2724 case ISN_EXECUTE:
2725 case ISN_FINALLY:
2726 case ISN_FINISH:
2727 case ISN_FOR:
2728 case ISN_GETITEM:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002729 case ISN_GET_OBJ_MEMBER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002730 case ISN_JUMP:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002731 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002732 case ISN_JUMP_IF_ARG_SET:
2733 case ISN_LISTAPPEND:
2734 case ISN_LISTINDEX:
2735 case ISN_LISTSLICE:
2736 case ISN_LOAD:
2737 case ISN_LOADBDICT:
2738 case ISN_LOADGDICT:
2739 case ISN_LOADOUTER:
2740 case ISN_LOADREG:
2741 case ISN_LOADTDICT:
2742 case ISN_LOADV:
2743 case ISN_LOADWDICT:
2744 case ISN_LOCKCONST:
2745 case ISN_MEMBER:
2746 case ISN_NEGATENR:
2747 case ISN_NEWDICT:
2748 case ISN_NEWLIST:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002749 case ISN_NEWPARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002750 case ISN_OPANY:
2751 case ISN_OPFLOAT:
2752 case ISN_OPNR:
2753 case ISN_PCALL:
2754 case ISN_PCALL_END:
2755 case ISN_PROF_END:
2756 case ISN_PROF_START:
2757 case ISN_PUSHBOOL:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002758 case ISN_PUSHCHANNEL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002759 case ISN_PUSHF:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002760 case ISN_PUSHJOB:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002761 case ISN_PUSHNR:
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002762 case ISN_PUSHOBJ:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002763 case ISN_PUSHSPEC:
2764 case ISN_PUT:
2765 case ISN_REDIREND:
2766 case ISN_REDIRSTART:
2767 case ISN_RETURN:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002768 case ISN_RETURN_OBJECT:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002769 case ISN_RETURN_VOID:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002770 case ISN_SHUFFLE:
2771 case ISN_SLICE:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002772 case ISN_SOURCE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002773 case ISN_STORE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002774 case ISN_STORENR:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002775 case ISN_STOREOUTER:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002776 case ISN_STORE_THIS:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002777 case ISN_STORERANGE:
2778 case ISN_STOREREG:
2779 case ISN_STOREV:
2780 case ISN_STRINDEX:
2781 case ISN_STRSLICE:
2782 case ISN_THROW:
2783 case ISN_TRYCONT:
2784 case ISN_UNLETINDEX:
2785 case ISN_UNLETRANGE:
2786 case ISN_UNPACK:
2787 case ISN_USEDICT:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002788 case ISN_WHILE:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002789 // nothing allocated
2790 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002791 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002792}
2793
2794 void
2795clear_instr_ga(garray_T *gap)
2796{
2797 int idx;
2798
2799 for (idx = 0; idx < gap->ga_len; ++idx)
2800 delete_instr(((isn_T *)gap->ga_data) + idx);
2801 ga_clear(gap);
2802}
2803
2804
2805#endif // defined(FEAT_EVAL)