blob: 52402c14ec8567c12f7890cf8f65bc93f790c6af [file] [log] [blame]
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9instr.c: Dealing with instructions of a compiled function
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19// When not generating protos this is included in proto.h
20#ifdef PROTO
21# include "vim9.h"
22#endif
23
24
25/////////////////////////////////////////////////////////////////////
26// Following generate_ functions expect the caller to call ga_grow().
27
28#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
29#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
30
31/*
32 * Generate an instruction without arguments.
33 * Returns a pointer to the new instruction, NULL if failed.
34 */
35 isn_T *
36generate_instr(cctx_T *cctx, isntype_T isn_type)
37{
38 garray_T *instr = &cctx->ctx_instr;
39 isn_T *isn;
40
41 RETURN_NULL_IF_SKIP(cctx);
42 if (GA_GROW_FAILS(instr, 1))
43 return NULL;
44 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
45 isn->isn_type = isn_type;
46 isn->isn_lnum = cctx->ctx_lnum + 1;
47 ++instr->ga_len;
48
49 return isn;
50}
51
52/*
53 * Generate an instruction without arguments.
54 * "drop" will be removed from the stack.
55 * Returns a pointer to the new instruction, NULL if failed.
56 */
57 isn_T *
58generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
59{
Bram Moolenaardc7c3662021-12-20 15:04:29 +000060 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +000061 cctx->ctx_type_stack.ga_len -= drop;
Bram Moolenaardc7c3662021-12-20 15:04:29 +000062 return generate_instr(cctx, isn_type);
63}
64
65/*
Bram Moolenaar078a4612022-01-04 15:17:03 +000066 * Generate instruction "isn_type" and put "type" on the type stack,
67 * use "decl_type" for the declared type.
Bram Moolenaardc7c3662021-12-20 15:04:29 +000068 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +000069 static isn_T *
Bram Moolenaar078a4612022-01-04 15:17:03 +000070generate_instr_type2(
71 cctx_T *cctx,
72 isntype_T isn_type,
73 type_T *type,
74 type_T *decl_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +000075{
76 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +000077
78 if ((isn = generate_instr(cctx, isn_type)) == NULL)
79 return NULL;
80
Bram Moolenaar078a4612022-01-04 15:17:03 +000081 if (push_type_stack2(cctx, type == NULL ? &t_any : type,
82 decl_type == NULL ? &t_any : decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +000083 return NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +000084
85 return isn;
86}
87
88/*
Bram Moolenaar078a4612022-01-04 15:17:03 +000089 * Generate instruction "isn_type" and put "type" on the type stack.
90 * Uses "any" for the declared type, which works for constants. For declared
91 * variables use generate_instr_type2().
92 */
93 isn_T *
94generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
95{
96 return generate_instr_type2(cctx, isn_type, type, &t_any);
97}
98
99/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000100 * Generate an ISN_DEBUG instruction.
101 */
102 isn_T *
103generate_instr_debug(cctx_T *cctx)
104{
105 isn_T *isn;
106 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
107 + cctx->ctx_ufunc->uf_dfunc_idx;
108
109 if ((isn = generate_instr(cctx, ISN_DEBUG)) == NULL)
110 return NULL;
111 isn->isn_arg.debug.dbg_var_names_len = dfunc->df_var_names.ga_len;
112 isn->isn_arg.debug.dbg_break_lnum = cctx->ctx_prev_lnum;
113 return isn;
114}
115
116/*
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000117 * Generate an ISN_CONSTRUCT instruction.
118 * The object will have "size" members.
119 */
120 int
121generate_CONSTRUCT(cctx_T *cctx, class_T *cl)
122{
123 isn_T *isn;
124
125 RETURN_OK_IF_SKIP(cctx);
126 if ((isn = generate_instr(cctx, ISN_CONSTRUCT)) == NULL)
127 return FAIL;
128 isn->isn_arg.construct.construct_size = sizeof(object_T)
129 + cl->class_obj_member_count * sizeof(typval_T);
130 isn->isn_arg.construct.construct_class = cl;
131 return OK;
132}
133
134/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000135 * Generate ISN_GET_OBJ_MEMBER - access member of object at bottom of stack by
136 * index.
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000137 */
138 int
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000139generate_GET_OBJ_MEMBER(cctx_T *cctx, int idx, type_T *type)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000140{
141 RETURN_OK_IF_SKIP(cctx);
142
143 // drop the object type
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000144 isn_T *isn = generate_instr_drop(cctx, ISN_GET_OBJ_MEMBER, 1);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000145 if (isn == NULL)
146 return FAIL;
147
148 isn->isn_arg.number = idx;
149 return push_type_stack2(cctx, type, &t_any);
150}
151
152/*
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000153 * Generate ISN_GET_ITF_MEMBER - access member of interface at bottom of stack
154 * by index.
155 */
156 int
157generate_GET_ITF_MEMBER(cctx_T *cctx, class_T *itf, int idx, type_T *type)
158{
159 RETURN_OK_IF_SKIP(cctx);
160
161 // drop the object type
162 isn_T *isn = generate_instr_drop(cctx, ISN_GET_ITF_MEMBER, 1);
163 if (isn == NULL)
164 return FAIL;
165
166 isn->isn_arg.classmember.cm_class = itf;
167 ++itf->class_refcount;
168 isn->isn_arg.classmember.cm_idx = idx;
169 return push_type_stack2(cctx, type, &t_any);
170}
171
172/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000173 * Generate ISN_STORE_THIS - store value in member of "this" object with member
174 * index "idx".
175 */
176 int
177generate_STORE_THIS(cctx_T *cctx, int idx)
178{
179 RETURN_OK_IF_SKIP(cctx);
180
181 // drop the value type
182 isn_T *isn = generate_instr_drop(cctx, ISN_STORE_THIS, 1);
183 if (isn == NULL)
184 return FAIL;
185
186 isn->isn_arg.number = idx;
187 return OK;
188}
189
190/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000191 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
192 * But only for simple types.
193 * When "tolerant" is TRUE convert most types to string, e.g. a List.
194 */
195 int
196may_generate_2STRING(int offset, int tolerant, cctx_T *cctx)
197{
198 isn_T *isn;
199 isntype_T isntype = ISN_2STRING;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000200 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000201
202 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +0000203 type = get_type_on_stack(cctx, -1 - offset);
204 switch (type->tt_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000205 {
206 // nothing to be done
207 case VAR_STRING: return OK;
208
209 // conversion possible
210 case VAR_SPECIAL:
211 case VAR_BOOL:
212 case VAR_NUMBER:
213 case VAR_FLOAT:
214 break;
215
216 // conversion possible (with runtime check)
217 case VAR_ANY:
218 case VAR_UNKNOWN:
219 isntype = ISN_2STRING_ANY;
220 break;
221
222 // conversion possible when tolerant
223 case VAR_LIST:
224 if (tolerant)
225 {
226 isntype = ISN_2STRING_ANY;
227 break;
228 }
229 // FALLTHROUGH
230
231 // conversion not possible
232 case VAR_VOID:
233 case VAR_BLOB:
234 case VAR_FUNC:
235 case VAR_PARTIAL:
236 case VAR_DICT:
237 case VAR_JOB:
238 case VAR_CHANNEL:
239 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000240 case VAR_CLASS:
241 case VAR_OBJECT:
Bram Moolenaar078a4612022-01-04 15:17:03 +0000242 to_string_error(type->tt_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000243 return FAIL;
244 }
245
Bram Moolenaar078a4612022-01-04 15:17:03 +0000246 set_type_on_stack(cctx, &t_string, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000247 if ((isn = generate_instr(cctx, isntype)) == NULL)
248 return FAIL;
249 isn->isn_arg.tostring.offset = offset;
250 isn->isn_arg.tostring.tolerant = tolerant;
251
252 return OK;
253}
254
255 static int
256check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
257{
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000258 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT
259 || type1 == VAR_ANY || type1 == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000260 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000261 || type2 == VAR_ANY || type2 == VAR_UNKNOWN)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000262 {
263 if (*op == '+')
264 emsg(_(e_wrong_argument_type_for_plus));
265 else
266 semsg(_(e_char_requires_number_or_float_arguments), *op);
267 return FAIL;
268 }
269 return OK;
270}
271
272/*
273 * Generate instruction for "+". For a list this creates a new list.
274 */
275 int
276generate_add_instr(
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000277 cctx_T *cctx,
278 vartype_T vartype,
279 type_T *type1,
280 type_T *type2,
281 exprtype_T expr_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000282{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000283 isn_T *isn = generate_instr_drop(cctx,
284 vartype == VAR_NUMBER ? ISN_OPNR
285 : vartype == VAR_LIST ? ISN_ADDLIST
286 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000287 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000288 : ISN_OPANY, 1);
289
290 if (vartype != VAR_LIST && vartype != VAR_BLOB
291 && type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000292 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000293 && type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000294 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000295 && check_number_or_float(
296 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
297 return FAIL;
298
299 if (isn != NULL)
300 {
301 if (isn->isn_type == ISN_ADDLIST)
302 isn->isn_arg.op.op_type = expr_type;
303 else
304 isn->isn_arg.op.op_type = EXPR_ADD;
305 }
306
307 // When concatenating two lists with different member types the member type
308 // becomes "any".
309 if (vartype == VAR_LIST
310 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
311 && type1->tt_member != type2->tt_member)
Bram Moolenaar078a4612022-01-04 15:17:03 +0000312 set_type_on_stack(cctx, &t_list_any, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000313
314 return isn == NULL ? FAIL : OK;
315}
316
317/*
318 * Get the type to use for an instruction for an operation on "type1" and
319 * "type2". If they are matching use a type-specific instruction. Otherwise
320 * fall back to runtime type checking.
321 */
322 vartype_T
323operator_type(type_T *type1, type_T *type2)
324{
325 if (type1->tt_type == type2->tt_type
326 && (type1->tt_type == VAR_NUMBER
327 || type1->tt_type == VAR_LIST
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000328 || type1->tt_type == VAR_FLOAT
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000329 || type1->tt_type == VAR_BLOB))
330 return type1->tt_type;
331 return VAR_ANY;
332}
333
334/*
335 * Generate an instruction with two arguments. The instruction depends on the
336 * type of the arguments.
337 */
338 int
339generate_two_op(cctx_T *cctx, char_u *op)
340{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000341 type_T *type1;
342 type_T *type2;
343 vartype_T vartype;
344 isn_T *isn;
345
346 RETURN_OK_IF_SKIP(cctx);
347
348 // Get the known type of the two items on the stack.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000349 type1 = get_type_on_stack(cctx, 1);
350 type2 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000351 vartype = operator_type(type1, type2);
352
353 switch (*op)
354 {
355 case '+':
356 if (generate_add_instr(cctx, vartype, type1, type2,
357 EXPR_COPY) == FAIL)
358 return FAIL;
359 break;
360
361 case '-':
362 case '*':
363 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
364 op) == FAIL)
365 return FAIL;
366 if (vartype == VAR_NUMBER)
367 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000368 else if (vartype == VAR_FLOAT)
369 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000370 else
371 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
372 if (isn != NULL)
373 isn->isn_arg.op.op_type = *op == '*'
374 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
375 break;
376
377 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000378 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000379 && type1->tt_type != VAR_NUMBER)
380 || (type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000381 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000382 && type2->tt_type != VAR_NUMBER))
383 {
384 emsg(_(e_percent_requires_number_arguments));
385 return FAIL;
386 }
387 isn = generate_instr_drop(cctx,
388 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
389 if (isn != NULL)
390 isn->isn_arg.op.op_type = EXPR_REM;
391 break;
392 }
393
394 // correct type of result
395 if (vartype == VAR_ANY)
396 {
397 type_T *type = &t_any;
398
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000399 // float+number and number+float results in float
400 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100401 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000402 type = &t_float;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000403 set_type_on_stack(cctx, type, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000404 }
405
406 return OK;
407}
408
409/*
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000410 * Get the instruction to use for comparing two values with specified types.
411 * Either "tv1" and "tv2" are passed or "type1" and "type2".
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000412 * Return ISN_DROP when failed.
413 */
414 static isntype_T
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000415get_compare_isn(
Bram Moolenaar2ed57ac2023-04-01 22:05:38 +0100416 exprtype_T exprtype,
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000417 typval_T *tv1,
418 typval_T *tv2,
419 type_T *type1,
420 type_T *type2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000421{
422 isntype_T isntype = ISN_DROP;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000423 vartype_T vartype1 = tv1 != NULL ? tv1->v_type : type1->tt_type;
424 vartype_T vartype2 = tv2 != NULL ? tv2->v_type : type2->tt_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000425
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000426 if (vartype1 == vartype2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000427 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000428 switch (vartype1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000429 {
430 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
431 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
432 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
433 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
434 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
435 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
436 case VAR_LIST: isntype = ISN_COMPARELIST; break;
437 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
438 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000439 case VAR_CLASS: isntype = ISN_COMPARECLASS; break;
440 case VAR_OBJECT: isntype = ISN_COMPAREOBJECT; break;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000441 default: isntype = ISN_COMPAREANY; break;
442 }
443 }
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000444 else if (vartype1 == VAR_ANY || vartype2 == VAR_ANY
445 || ((vartype1 == VAR_NUMBER || vartype1 == VAR_FLOAT)
446 && (vartype2 == VAR_NUMBER || vartype2 == VAR_FLOAT))
447 || (vartype1 == VAR_FUNC && vartype2 == VAR_PARTIAL)
448 || (vartype1 == VAR_PARTIAL && vartype2 == VAR_FUNC))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000449 isntype = ISN_COMPAREANY;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000450 else if (vartype1 == VAR_SPECIAL || vartype2 == VAR_SPECIAL)
Bram Moolenaar7a222242022-03-01 19:23:24 +0000451 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000452 if ((vartype1 == VAR_SPECIAL
453 && (tv1 != NULL ? tv1->vval.v_number == VVAL_NONE
454 : type1 == &t_none)
455 && vartype2 != VAR_STRING)
456 || (vartype2 == VAR_SPECIAL
457 && (tv2 != NULL ? tv2->vval.v_number == VVAL_NONE
458 : type2 == &t_none)
459 && vartype1 != VAR_STRING))
460 {
461 semsg(_(e_cannot_compare_str_with_str),
462 vartype_name(vartype1), vartype_name(vartype2));
463 return ISN_DROP;
464 }
Bram Moolenaar05667812022-03-15 20:21:33 +0000465 // although comparing null with number, float or bool is not useful, we
466 // allow it
Bram Moolenaar7a222242022-03-01 19:23:24 +0000467 isntype = ISN_COMPARENULL;
468 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000469
470 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
471 && (isntype == ISN_COMPAREBOOL
472 || isntype == ISN_COMPARESPECIAL
473 || isntype == ISN_COMPARENR
474 || isntype == ISN_COMPAREFLOAT))
475 {
476 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000477 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(vartype1));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000478 return ISN_DROP;
479 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000480 if (!(exprtype == EXPR_IS || exprtype == EXPR_ISNOT
481 || exprtype == EXPR_EQUAL || exprtype == EXPR_NEQUAL)
482 && (isntype == ISN_COMPAREOBJECT || isntype == ISN_COMPARECLASS))
483 {
484 semsg(_(e_invalid_operation_for_str), vartype_name(vartype1));
485 return ISN_DROP;
486 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000487 if (isntype == ISN_DROP
Bram Moolenaar2ed57ac2023-04-01 22:05:38 +0100488 || (isntype != ISN_COMPARENULL
489 && (((exprtype != EXPR_EQUAL
490 && exprtype != EXPR_NEQUAL
491 && (vartype1 == VAR_BOOL || vartype1 == VAR_SPECIAL
492 || vartype2 == VAR_BOOL || vartype2 == VAR_SPECIAL)))
493 || ((exprtype != EXPR_EQUAL
494 && exprtype != EXPR_NEQUAL
495 && exprtype != EXPR_IS
496 && exprtype != EXPR_ISNOT
497 && (vartype1 == VAR_BLOB || vartype2 == VAR_BLOB
498 || vartype1 == VAR_LIST || vartype2 == VAR_LIST))))))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000499 {
500 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000501 vartype_name(vartype1), vartype_name(vartype2));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000502 return ISN_DROP;
503 }
504 return isntype;
505}
506
507 int
508check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
509{
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000510 if (get_compare_isn(type, tv1, tv2, NULL, NULL) == ISN_DROP)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000511 return FAIL;
512 return OK;
513}
514
515/*
516 * Generate an ISN_COMPARE* instruction with a boolean result.
517 */
518 int
519generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
520{
521 isntype_T isntype;
522 isn_T *isn;
523 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000524
525 RETURN_OK_IF_SKIP(cctx);
526
527 // Get the known type of the two items on the stack. If they are matching
528 // use a type-specific instruction. Otherwise fall back to runtime type
529 // checking.
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000530 isntype = get_compare_isn(exprtype, NULL, NULL,
531 get_type_on_stack(cctx, 1), get_type_on_stack(cctx, 0));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000532 if (isntype == ISN_DROP)
533 return FAIL;
534
535 if ((isn = generate_instr(cctx, isntype)) == NULL)
536 return FAIL;
537 isn->isn_arg.op.op_type = exprtype;
538 isn->isn_arg.op.op_ic = ic;
539
540 // takes two arguments, puts one bool back
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100541 --stack->ga_len;
542 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000543
544 return OK;
545}
546
547/*
LemonBoy372bcce2022-04-25 12:43:20 +0100548 * Generate an ISN_CONCAT instruction.
549 * "count" is the number of stack elements to join together and it must be
550 * greater or equal to one.
551 * The caller ensures all the "count" elements on the stack have the right type.
552 */
553 int
554generate_CONCAT(cctx_T *cctx, int count)
555{
556 isn_T *isn;
557 garray_T *stack = &cctx->ctx_type_stack;
558
559 RETURN_OK_IF_SKIP(cctx);
560
LemonBoy372bcce2022-04-25 12:43:20 +0100561 if ((isn = generate_instr(cctx, ISN_CONCAT)) == NULL)
562 return FAIL;
563 isn->isn_arg.number = count;
564
565 // drop the argument types
566 stack->ga_len -= count - 1;
567
568 return OK;
569}
570
571/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000572 * Generate an ISN_2BOOL instruction.
573 * "offset" is the offset in the type stack.
574 */
575 int
576generate_2BOOL(cctx_T *cctx, int invert, int offset)
577{
578 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000579
580 RETURN_OK_IF_SKIP(cctx);
581 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
582 return FAIL;
583 isn->isn_arg.tobool.invert = invert;
584 isn->isn_arg.tobool.offset = offset;
585
586 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000587 set_type_on_stack(cctx, &t_bool, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000588
589 return OK;
590}
591
592/*
593 * Generate an ISN_COND2BOOL instruction.
594 */
595 int
596generate_COND2BOOL(cctx_T *cctx)
597{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000598 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100599 if (generate_instr(cctx, ISN_COND2BOOL) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000600 return FAIL;
601
602 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000603 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000604
605 return OK;
606}
607
608 int
609generate_TYPECHECK(
610 cctx_T *cctx,
611 type_T *expected,
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000612 int number_ok, // add TTFLAG_NUMBER_OK flag
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000613 int offset,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100614 int is_var,
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000615 int argidx)
616{
617 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000618
619 RETURN_OK_IF_SKIP(cctx);
620 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
621 return FAIL;
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000622 type_T *tt;
623 if (expected->tt_type == VAR_FLOAT && number_ok)
624 {
625 // always allocate, also for static types
626 tt = ALLOC_ONE(type_T);
627 if (tt != NULL)
628 {
629 *tt = *expected;
Bram Moolenaarc4b3f642022-12-30 10:36:34 +0000630 tt->tt_flags &= ~TTFLAG_STATIC;
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000631 tt->tt_flags |= TTFLAG_NUMBER_OK;
632 }
633 }
634 else
635 tt = alloc_type(expected);
636
637 isn->isn_arg.type.ct_type = tt;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000638 isn->isn_arg.type.ct_off = (int8_T)offset;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100639 isn->isn_arg.type.ct_is_var = is_var;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000640 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
641
642 // type becomes expected
Bram Moolenaar078a4612022-01-04 15:17:03 +0000643 set_type_on_stack(cctx, expected, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000644
645 return OK;
646}
647
648 int
649generate_SETTYPE(
650 cctx_T *cctx,
651 type_T *expected)
652{
653 isn_T *isn;
654
655 RETURN_OK_IF_SKIP(cctx);
656 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
657 return FAIL;
658 isn->isn_arg.type.ct_type = alloc_type(expected);
659 return OK;
660}
661
662/*
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000663 * Generate an ISN_PUSHOBJ instruction. Object is always NULL.
664 */
665 static int
666generate_PUSHOBJ(cctx_T *cctx)
667{
668 RETURN_OK_IF_SKIP(cctx);
669 if (generate_instr_type(cctx, ISN_PUSHOBJ, &t_any) == NULL)
670 return FAIL;
671 return OK;
672}
673
674/*
675 * Generate an ISN_PUSHCLASS instruction. "class" can be NULL.
676 */
677 static int
678generate_PUSHCLASS(cctx_T *cctx, class_T *class)
679{
680 RETURN_OK_IF_SKIP(cctx);
681 isn_T *isn = generate_instr_type(cctx, ISN_PUSHCLASS,
682 class == NULL ? &t_any : &class->class_type);
683 if (isn == NULL)
684 return FAIL;
Bram Moolenaar30a84472023-02-27 08:07:14 +0000685 isn->isn_arg.classarg = class;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000686 if (class != NULL)
687 ++class->class_refcount;
688 return OK;
689}
690
691/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000692 * Generate a PUSH instruction for "tv".
693 * "tv" will be consumed or cleared.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000694 */
695 int
696generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
697{
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100698 switch (tv->v_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000699 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100700 case VAR_BOOL:
701 generate_PUSHBOOL(cctx, tv->vval.v_number);
702 break;
703 case VAR_SPECIAL:
704 generate_PUSHSPEC(cctx, tv->vval.v_number);
705 break;
706 case VAR_NUMBER:
707 generate_PUSHNR(cctx, tv->vval.v_number);
708 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100709 case VAR_FLOAT:
710 generate_PUSHF(cctx, tv->vval.v_float);
711 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100712 case VAR_BLOB:
713 generate_PUSHBLOB(cctx, tv->vval.v_blob);
714 tv->vval.v_blob = NULL;
715 break;
716 case VAR_LIST:
717 if (tv->vval.v_list != NULL)
718 iemsg("non-empty list constant not supported");
719 generate_NEWLIST(cctx, 0, TRUE);
720 break;
721 case VAR_DICT:
722 if (tv->vval.v_dict != NULL)
723 iemsg("non-empty dict constant not supported");
724 generate_NEWDICT(cctx, 0, TRUE);
725 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000726#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100727 case VAR_JOB:
728 if (tv->vval.v_job != NULL)
729 iemsg("non-null job constant not supported");
730 generate_PUSHJOB(cctx);
731 break;
732 case VAR_CHANNEL:
733 if (tv->vval.v_channel != NULL)
734 iemsg("non-null channel constant not supported");
735 generate_PUSHCHANNEL(cctx);
736 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000737#endif
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100738 case VAR_FUNC:
739 if (tv->vval.v_string != NULL)
740 iemsg("non-null function constant not supported");
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100741 generate_PUSHFUNC(cctx, NULL, &t_func_unknown, TRUE);
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100742 break;
743 case VAR_PARTIAL:
744 if (tv->vval.v_partial != NULL)
745 iemsg("non-null partial constant not supported");
746 if (generate_instr_type(cctx, ISN_NEWPARTIAL, &t_func_unknown)
747 == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000748 return FAIL;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100749 break;
750 case VAR_STRING:
751 generate_PUSHS(cctx, &tv->vval.v_string);
752 tv->vval.v_string = NULL;
753 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000754 case VAR_OBJECT:
755 if (tv->vval.v_object != NULL)
756 {
757 emsg(_(e_cannot_use_non_null_object));
758 return FAIL;
759 }
760 generate_PUSHOBJ(cctx);
761 break;
762 case VAR_CLASS:
763 generate_PUSHCLASS(cctx, tv->vval.v_class);
764 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100765 default:
766 siemsg("constant type %d not supported", tv->v_type);
767 clear_tv(tv);
768 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000769 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100770 tv->v_type = VAR_UNKNOWN;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000771 return OK;
772}
773
774/*
775 * Generate an ISN_PUSHNR instruction.
776 */
777 int
778generate_PUSHNR(cctx_T *cctx, varnumber_T number)
779{
780 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000781
782 RETURN_OK_IF_SKIP(cctx);
783 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
784 return FAIL;
785 isn->isn_arg.number = number;
786
787 if (number == 0 || number == 1)
788 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000789 set_type_on_stack(cctx, &t_number_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000790 return OK;
791}
792
793/*
794 * Generate an ISN_PUSHBOOL instruction.
795 */
796 int
797generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
798{
799 isn_T *isn;
800
801 RETURN_OK_IF_SKIP(cctx);
802 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
803 return FAIL;
804 isn->isn_arg.number = number;
805
806 return OK;
807}
808
809/*
810 * Generate an ISN_PUSHSPEC instruction.
811 */
812 int
813generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
814{
815 isn_T *isn;
816
817 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000818 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC,
819 number == VVAL_NULL ? &t_null : &t_none)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000820 return FAIL;
821 isn->isn_arg.number = number;
822
823 return OK;
824}
825
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000826/*
827 * Generate an ISN_PUSHF instruction.
828 */
829 int
830generate_PUSHF(cctx_T *cctx, float_T fnumber)
831{
832 isn_T *isn;
833
834 RETURN_OK_IF_SKIP(cctx);
835 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
836 return FAIL;
837 isn->isn_arg.fnumber = fnumber;
838
839 return OK;
840}
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000841
842/*
843 * Generate an ISN_PUSHS instruction.
844 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100845 * Note that if "str" is used in the instruction OK is returned and "*str" is
846 * not set to NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000847 */
848 int
849generate_PUSHS(cctx_T *cctx, char_u **str)
850{
851 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100852 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000853
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100854 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000855 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100856 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
857 ret = FAIL;
858 else
859 {
860 isn->isn_arg.string = str == NULL ? NULL : *str;
861 return OK;
862 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000863 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100864 if (str != NULL)
865 VIM_CLEAR(*str);
866 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000867}
868
869/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000870 * Generate an ISN_PUSHCHANNEL instruction. Channel is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000871 */
872 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000873generate_PUSHCHANNEL(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000874{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000875 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100876#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100877 if (generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000878 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000879 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100880#else
881 emsg(_(e_channel_job_feature_not_available));
882 return FAIL;
883#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000884}
885
886/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000887 * Generate an ISN_PUSHJOB instruction. Job is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000888 */
889 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000890generate_PUSHJOB(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000891{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000892 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100893#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100894 if (generate_instr_type(cctx, ISN_PUSHJOB, &t_job) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000895 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000896 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100897#else
898 emsg(_(e_channel_job_feature_not_available));
899 return FAIL;
900#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000901}
902
903/*
904 * Generate an ISN_PUSHBLOB instruction.
905 * Consumes "blob".
906 */
907 int
908generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
909{
910 isn_T *isn;
911
912 RETURN_OK_IF_SKIP(cctx);
913 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
914 return FAIL;
915 isn->isn_arg.blob = blob;
916
917 return OK;
918}
919
920/*
921 * Generate an ISN_PUSHFUNC instruction with name "name".
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100922 * When "may_prefix" is TRUE prefix "g:" unless "name" is script-local or
923 * autoload.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000924 */
925 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100926generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type, int may_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000927{
928 isn_T *isn;
929 char_u *funcname;
930
931 RETURN_OK_IF_SKIP(cctx);
932 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
933 return FAIL;
934 if (name == NULL)
935 funcname = NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100936 else if (!may_prefix
937 || *name == K_SPECIAL // script-local
Bram Moolenaard041f422022-01-12 19:54:00 +0000938 || vim_strchr(name, AUTOLOAD_CHAR) != NULL) // autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000939 funcname = vim_strsave(name);
940 else
941 {
942 funcname = alloc(STRLEN(name) + 3);
943 if (funcname != NULL)
944 {
945 STRCPY(funcname, "g:");
946 STRCPY(funcname + 2, name);
947 }
948 }
949
950 isn->isn_arg.string = funcname;
951 return OK;
952}
953
954/*
Bram Moolenaar06b77222022-01-25 15:51:56 +0000955 * Generate an ISN_AUTOLOAD instruction.
956 */
957 int
958generate_AUTOLOAD(cctx_T *cctx, char_u *name, type_T *type)
959{
960 isn_T *isn;
961
962 RETURN_OK_IF_SKIP(cctx);
963 if ((isn = generate_instr_type(cctx, ISN_AUTOLOAD, type)) == NULL)
964 return FAIL;
965 isn->isn_arg.string = vim_strsave(name);
966 if (isn->isn_arg.string == NULL)
967 return FAIL;
968 return OK;
969}
970
971/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000972 * Generate an ISN_GETITEM instruction with "index".
973 * "with_op" is TRUE for "+=" and other operators, the stack has the current
974 * value below the list with values.
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100975 * Caller must check the type is a list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000976 */
977 int
978generate_GETITEM(cctx_T *cctx, int index, int with_op)
979{
980 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000981 type_T *type = get_type_on_stack(cctx, with_op ? 1 : 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000982 type_T *item_type = &t_any;
983
984 RETURN_OK_IF_SKIP(cctx);
985
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000986 item_type = type->tt_member;
987 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
988 return FAIL;
989 isn->isn_arg.getitem.gi_index = index;
990 isn->isn_arg.getitem.gi_with_op = with_op;
991
992 // add the item type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +0000993 return push_type_stack(cctx, item_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000994}
995
996/*
997 * Generate an ISN_SLICE instruction with "count".
998 */
999 int
1000generate_SLICE(cctx_T *cctx, int count)
1001{
1002 isn_T *isn;
1003
1004 RETURN_OK_IF_SKIP(cctx);
1005 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1006 return FAIL;
1007 isn->isn_arg.number = count;
1008 return OK;
1009}
1010
1011/*
1012 * Generate an ISN_CHECKLEN instruction with "min_len".
1013 */
1014 int
1015generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1016{
1017 isn_T *isn;
1018
1019 RETURN_OK_IF_SKIP(cctx);
1020
1021 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1022 return FAIL;
1023 isn->isn_arg.checklen.cl_min_len = min_len;
1024 isn->isn_arg.checklen.cl_more_OK = more_OK;
1025
1026 return OK;
1027}
1028
1029/*
1030 * Generate an ISN_STORE instruction.
1031 */
1032 int
1033generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1034{
1035 isn_T *isn;
1036
1037 RETURN_OK_IF_SKIP(cctx);
1038 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1039 return FAIL;
1040 if (name != NULL)
1041 isn->isn_arg.string = vim_strsave(name);
1042 else
1043 isn->isn_arg.number = idx;
1044
1045 return OK;
1046}
1047
1048/*
Bram Moolenaard505d172022-12-18 21:42:55 +00001049 * Generate an ISN_LOAD_CLASSMEMBER ("load" == TRUE) or ISN_STORE_CLASSMEMBER
1050 * ("load" == FALSE) instruction.
1051 */
1052 int
1053generate_CLASSMEMBER(
1054 cctx_T *cctx,
1055 int load,
1056 class_T *cl,
1057 int idx)
1058{
1059 isn_T *isn;
1060
1061 RETURN_OK_IF_SKIP(cctx);
1062 if (load)
1063 {
1064 ocmember_T *m = &cl->class_class_members[idx];
1065 isn = generate_instr_type(cctx, ISN_LOAD_CLASSMEMBER, m->ocm_type);
1066 }
1067 else
1068 {
1069 isn = generate_instr_drop(cctx, ISN_STORE_CLASSMEMBER, 1);
1070 }
1071 if (isn == NULL)
1072 return FAIL;
1073 isn->isn_arg.classmember.cm_class = cl;
1074 ++cl->class_refcount;
1075 isn->isn_arg.classmember.cm_idx = idx;
1076
1077 return OK;
1078}
1079
1080/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001081 * Generate an ISN_STOREOUTER instruction.
1082 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +00001083 static int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001084generate_STOREOUTER(cctx_T *cctx, int idx, int level, int loop_idx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001085{
1086 isn_T *isn;
1087
1088 RETURN_OK_IF_SKIP(cctx);
1089 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1090 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001091 if (level == 1 && loop_idx >= 0 && idx >= loop_idx)
1092 {
1093 // Store a variable defined in a loop. A copy will be made at the end
1094 // of the loop. TODO: how about deeper nesting?
1095 isn->isn_arg.outer.outer_idx = idx - loop_idx;
1096 isn->isn_arg.outer.outer_depth = OUTER_LOOP_DEPTH;
1097 }
1098 else
1099 {
1100 isn->isn_arg.outer.outer_idx = idx;
1101 isn->isn_arg.outer.outer_depth = level;
1102 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001103
1104 return OK;
1105}
1106
1107/*
1108 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1109 */
1110 int
1111generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1112{
1113 isn_T *isn;
1114
1115 RETURN_OK_IF_SKIP(cctx);
1116 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1117 return FAIL;
1118 isn->isn_arg.storenr.stnr_idx = idx;
1119 isn->isn_arg.storenr.stnr_val = value;
1120
1121 return OK;
1122}
1123
1124/*
1125 * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction
1126 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +00001127 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001128generate_STOREOPT(
1129 cctx_T *cctx,
1130 isntype_T isn_type,
1131 char_u *name,
1132 int opt_flags)
1133{
1134 isn_T *isn;
1135
1136 RETURN_OK_IF_SKIP(cctx);
1137 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1138 return FAIL;
1139 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1140 isn->isn_arg.storeopt.so_flags = opt_flags;
1141
1142 return OK;
1143}
1144
1145/*
1146 * Generate an ISN_LOAD or similar instruction.
1147 */
1148 int
1149generate_LOAD(
1150 cctx_T *cctx,
1151 isntype_T isn_type,
1152 int idx,
1153 char_u *name,
1154 type_T *type)
1155{
1156 isn_T *isn;
1157
1158 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001159 if ((isn = generate_instr_type2(cctx, isn_type, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001160 return FAIL;
1161 if (name != NULL)
1162 isn->isn_arg.string = vim_strsave(name);
1163 else
1164 isn->isn_arg.number = idx;
1165
1166 return OK;
1167}
1168
1169/*
1170 * Generate an ISN_LOADOUTER instruction
1171 */
1172 int
1173generate_LOADOUTER(
1174 cctx_T *cctx,
1175 int idx,
1176 int nesting,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001177 int loop_depth,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001178 int loop_idx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001179 type_T *type)
1180{
1181 isn_T *isn;
1182
1183 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001184 if ((isn = generate_instr_type2(cctx, ISN_LOADOUTER, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001185 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001186 if (nesting == 1 && loop_idx >= 0 && idx >= loop_idx)
1187 {
1188 // Load a variable defined in a loop. A copy will be made at the end
Bram Moolenaarcc341812022-09-19 15:54:34 +01001189 // of the loop.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001190 isn->isn_arg.outer.outer_idx = idx - loop_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001191 isn->isn_arg.outer.outer_depth = -loop_depth - 1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001192 }
1193 else
1194 {
1195 isn->isn_arg.outer.outer_idx = idx;
1196 isn->isn_arg.outer.outer_depth = nesting;
1197 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001198
1199 return OK;
1200}
1201
1202/*
1203 * Generate an ISN_LOADV instruction for v:var.
1204 */
1205 int
1206generate_LOADV(
1207 cctx_T *cctx,
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001208 char_u *name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001209{
1210 int di_flags;
1211 int vidx = find_vim_var(name, &di_flags);
1212 type_T *type;
1213
1214 RETURN_OK_IF_SKIP(cctx);
1215 if (vidx < 0)
1216 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001217 semsg(_(e_variable_not_found_str), name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001218 return FAIL;
1219 }
Bram Moolenaard787e402021-12-24 21:36:12 +00001220 type = get_vim_var_type(vidx, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001221 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
1222}
1223
1224/*
1225 * Generate an ISN_UNLET instruction.
1226 */
1227 int
1228generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
1229{
1230 isn_T *isn;
1231
1232 RETURN_OK_IF_SKIP(cctx);
1233 if ((isn = generate_instr(cctx, isn_type)) == NULL)
1234 return FAIL;
1235 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1236 isn->isn_arg.unlet.ul_forceit = forceit;
1237
1238 return OK;
1239}
1240
1241/*
1242 * Generate an ISN_LOCKCONST instruction.
1243 */
1244 int
1245generate_LOCKCONST(cctx_T *cctx)
1246{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001247 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01001248 if (generate_instr(cctx, ISN_LOCKCONST) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001249 return FAIL;
1250 return OK;
1251}
1252
1253/*
1254 * Generate an ISN_LOADS instruction.
1255 */
1256 int
1257generate_OLDSCRIPT(
1258 cctx_T *cctx,
1259 isntype_T isn_type,
1260 char_u *name,
1261 int sid,
1262 type_T *type)
1263{
1264 isn_T *isn;
1265
1266 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001267 if (isn_type == ISN_LOADS || isn_type == ISN_LOADEXPORT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001268 isn = generate_instr_type(cctx, isn_type, type);
1269 else
1270 isn = generate_instr_drop(cctx, isn_type, 1);
1271 if (isn == NULL)
1272 return FAIL;
1273 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1274 isn->isn_arg.loadstore.ls_sid = sid;
1275
1276 return OK;
1277}
1278
1279/*
1280 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1281 */
1282 int
1283generate_VIM9SCRIPT(
1284 cctx_T *cctx,
1285 isntype_T isn_type,
1286 int sid,
1287 int idx,
1288 type_T *type)
1289{
1290 isn_T *isn;
1291 scriptref_T *sref;
1292 scriptitem_T *si = SCRIPT_ITEM(sid);
1293
1294 RETURN_OK_IF_SKIP(cctx);
1295 if (isn_type == ISN_LOADSCRIPT)
Bram Moolenaar160afdb2022-02-06 17:17:02 +00001296 isn = generate_instr_type2(cctx, isn_type, type, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001297 else
1298 isn = generate_instr_drop(cctx, isn_type, 1);
1299 if (isn == NULL)
1300 return FAIL;
1301
1302 // This requires three arguments, which doesn't fit in an instruction, thus
1303 // we need to allocate a struct for this.
1304 sref = ALLOC_ONE(scriptref_T);
1305 if (sref == NULL)
1306 return FAIL;
1307 isn->isn_arg.script.scriptref = sref;
1308 sref->sref_sid = sid;
1309 sref->sref_idx = idx;
1310 sref->sref_seq = si->sn_script_seq;
1311 sref->sref_type = type;
1312 return OK;
1313}
1314
1315/*
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001316 * Generate an ISN_NEWLIST instruction for "count" items.
1317 * "use_null" is TRUE for null_list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001318 */
1319 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001320generate_NEWLIST(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001321{
1322 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001323 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001324 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001325 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001326
1327 RETURN_OK_IF_SKIP(cctx);
1328 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1329 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001330 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001331
Bram Moolenaar078a4612022-01-04 15:17:03 +00001332 // Get the member type and the declared member type from all the items on
1333 // the stack.
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001334 member_type = get_member_type_from_stack(count, 1, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001335 type = get_list_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001336 decl_type = get_list_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001337
1338 // drop the value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001339 cctx->ctx_type_stack.ga_len -= count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001340
1341 // add the list type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001342 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001343}
1344
1345/*
1346 * Generate an ISN_NEWDICT instruction.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001347 * "use_null" is TRUE for null_dict.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001348 */
1349 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001350generate_NEWDICT(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001351{
1352 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001353 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001354 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001355 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001356
1357 RETURN_OK_IF_SKIP(cctx);
1358 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1359 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001360 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001361
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001362 member_type = get_member_type_from_stack(count, 2, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001363 type = get_dict_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001364 decl_type = get_dict_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001365
1366 // drop the key and value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001367 cctx->ctx_type_stack.ga_len -= 2 * count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001368
1369 // add the dict type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001370 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001371}
1372
1373/*
1374 * Generate an ISN_FUNCREF instruction.
Bram Moolenaar313e4722023-02-08 20:55:27 +00001375 * For "obj.Method" "cl" is the class of the object (can be an interface or a
1376 * base class) and "fi" the index of the method on that class.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001377 * "isnp" is set to the instruction, so that fr_dfunc_idx can be set later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001378 */
1379 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001380generate_FUNCREF(
1381 cctx_T *cctx,
1382 ufunc_T *ufunc,
Bram Moolenaar313e4722023-02-08 20:55:27 +00001383 class_T *cl,
1384 int fi,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001385 isn_T **isnp)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001386{
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001387 isn_T *isn;
1388 type_T *type;
1389 funcref_extra_T *extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001390 loopvarinfo_T loopinfo;
1391 int has_vars;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001392
1393 RETURN_OK_IF_SKIP(cctx);
1394 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1395 return FAIL;
Bram Moolenaara915fa02022-03-23 11:29:15 +00001396 if (isnp != NULL)
1397 *isnp = isn;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001398
Bram Moolenaarcc341812022-09-19 15:54:34 +01001399 has_vars = get_loop_var_info(cctx, &loopinfo);
Bram Moolenaar313e4722023-02-08 20:55:27 +00001400 if (ufunc->uf_def_status == UF_NOT_COMPILED || has_vars || cl != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001401 {
1402 extra = ALLOC_CLEAR_ONE(funcref_extra_T);
1403 if (extra == NULL)
1404 return FAIL;
1405 isn->isn_arg.funcref.fr_extra = extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001406 extra->fre_loopvar_info = loopinfo;
Bram Moolenaar313e4722023-02-08 20:55:27 +00001407 if (cl != NULL)
1408 {
1409 extra->fre_class = cl;
1410 ++cl->class_refcount;
1411 extra->fre_method_idx = fi;
1412 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001413 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00001414 if (ufunc->uf_def_status == UF_NOT_COMPILED || cl != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001415 extra->fre_func_name = vim_strsave(ufunc->uf_name);
Bram Moolenaar313e4722023-02-08 20:55:27 +00001416 if (ufunc->uf_def_status != UF_NOT_COMPILED && cl == NULL)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001417 {
1418 if (isnp == NULL && ufunc->uf_def_status == UF_TO_BE_COMPILED)
1419 // compile the function now, we need the uf_dfunc_idx value
1420 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001421 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001422 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01001423
1424 // Reserve an extra variable to keep track of the number of closures
1425 // created.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001426 cctx->ctx_has_closure = 1;
1427
1428 // If the referenced function is a closure, it may use items further up in
Bram Moolenaar139575d2022-03-15 19:29:30 +00001429 // the nested context, including this one. But not a function defined at
1430 // the script level.
1431 if ((ufunc->uf_flags & FC_CLOSURE)
1432 && func_name_refcount(cctx->ctx_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001433 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1434
Bram Moolenaar078a4612022-01-04 15:17:03 +00001435 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1436 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001437}
1438
1439/*
1440 * Generate an ISN_NEWFUNC instruction.
1441 * "lambda_name" and "func_name" must be in allocated memory and will be
1442 * consumed.
1443 */
1444 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001445generate_NEWFUNC(
1446 cctx_T *cctx,
1447 char_u *lambda_name,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001448 char_u *func_name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001449{
1450 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001451 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001452
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001453 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001454 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001455 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1456 ret = FAIL;
1457 else
1458 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001459 newfuncarg_T *arg = ALLOC_CLEAR_ONE(newfuncarg_T);
1460
1461 if (arg == NULL)
1462 ret = FAIL;
1463 else
1464 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01001465 // Reserve an extra variable to keep track of the number of
1466 // closures created.
1467 cctx->ctx_has_closure = 1;
1468
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001469 isn->isn_arg.newfunc.nf_arg = arg;
1470 arg->nfa_lambda = lambda_name;
1471 arg->nfa_global = func_name;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001472 (void)get_loop_var_info(cctx, &arg->nfa_loopvar_info);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001473 return OK;
1474 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001475 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001476 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001477 vim_free(lambda_name);
1478 vim_free(func_name);
1479 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001480}
1481
1482/*
1483 * Generate an ISN_DEF instruction: list functions
1484 */
1485 int
1486generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1487{
1488 isn_T *isn;
1489
1490 RETURN_OK_IF_SKIP(cctx);
1491 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1492 return FAIL;
1493 if (len > 0)
1494 {
1495 isn->isn_arg.string = vim_strnsave(name, len);
1496 if (isn->isn_arg.string == NULL)
1497 return FAIL;
1498 }
1499 return OK;
1500}
1501
1502/*
1503 * Generate an ISN_JUMP instruction.
1504 */
1505 int
1506generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1507{
1508 isn_T *isn;
1509 garray_T *stack = &cctx->ctx_type_stack;
1510
1511 RETURN_OK_IF_SKIP(cctx);
1512 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1513 return FAIL;
1514 isn->isn_arg.jump.jump_when = when;
1515 isn->isn_arg.jump.jump_where = where;
1516
1517 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1518 --stack->ga_len;
1519
1520 return OK;
1521}
1522
1523/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001524 * Generate an ISN_WHILE instruction. Similar to ISN_JUMP for :while
1525 */
1526 int
1527generate_WHILE(cctx_T *cctx, int funcref_idx)
1528{
1529 isn_T *isn;
1530 garray_T *stack = &cctx->ctx_type_stack;
1531
1532 RETURN_OK_IF_SKIP(cctx);
1533 if ((isn = generate_instr(cctx, ISN_WHILE)) == NULL)
1534 return FAIL;
1535 isn->isn_arg.whileloop.while_funcref_idx = funcref_idx;
1536 isn->isn_arg.whileloop.while_end = 0; // filled in later
1537
1538 if (stack->ga_len > 0)
1539 --stack->ga_len;
1540
1541 return OK;
1542}
1543
1544/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001545 * Generate an ISN_JUMP_IF_ARG_SET or ISN_JUMP_IF_ARG_NOT_SET instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001546 */
1547 int
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001548generate_JUMP_IF_ARG(cctx_T *cctx, isntype_T isn_type, int arg_off)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001549{
1550 isn_T *isn;
1551
1552 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001553 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001554 return FAIL;
1555 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1556 // jump_where is set later
1557 return OK;
1558}
1559
1560 int
1561generate_FOR(cctx_T *cctx, int loop_idx)
1562{
1563 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001564
1565 RETURN_OK_IF_SKIP(cctx);
1566 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1567 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001568 isn->isn_arg.forloop.for_loop_idx = loop_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001569
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001570 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001571 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001572}
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001573
1574 int
Bram Moolenaarcc341812022-09-19 15:54:34 +01001575generate_ENDLOOP(cctx_T *cctx, loop_info_T *loop_info)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001576{
1577 isn_T *isn;
1578
1579 RETURN_OK_IF_SKIP(cctx);
1580 if ((isn = generate_instr(cctx, ISN_ENDLOOP)) == NULL)
1581 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001582 isn->isn_arg.endloop.end_depth = loop_info->li_depth;
1583 isn->isn_arg.endloop.end_funcref_idx = loop_info->li_funcref_idx;
1584 isn->isn_arg.endloop.end_var_idx = loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001585 isn->isn_arg.endloop.end_var_count =
Bram Moolenaarcc341812022-09-19 15:54:34 +01001586 cctx->ctx_locals.ga_len - loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001587 return OK;
1588}
1589
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001590/*
1591 * Generate an ISN_TRYCONT instruction.
1592 */
1593 int
1594generate_TRYCONT(cctx_T *cctx, int levels, int where)
1595{
1596 isn_T *isn;
1597
1598 RETURN_OK_IF_SKIP(cctx);
1599 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1600 return FAIL;
1601 isn->isn_arg.trycont.tct_levels = levels;
1602 isn->isn_arg.trycont.tct_where = where;
1603
1604 return OK;
1605}
1606
Bram Moolenaar16900322022-09-08 19:51:45 +01001607/*
1608 * Check "argount" arguments and their types on the type stack.
1609 * Give an error and return FAIL if something is wrong.
1610 * When "method_call" is NULL no code is generated.
1611 */
1612 int
1613check_internal_func_args(
1614 cctx_T *cctx,
1615 int func_idx,
1616 int argcount,
1617 int method_call,
1618 type2_T **argtypes,
1619 type2_T *shuffled_argtypes)
1620{
1621 garray_T *stack = &cctx->ctx_type_stack;
1622 int argoff = check_internal_func(func_idx, argcount);
1623
1624 if (argoff < 0)
1625 return FAIL;
1626
1627 if (method_call && argoff > 1)
1628 {
1629 isn_T *isn = generate_instr(cctx, ISN_SHUFFLE);
1630
1631 if (isn == NULL)
1632 return FAIL;
1633 isn->isn_arg.shuffle.shfl_item = argcount;
1634 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1635 }
1636
1637 if (argcount > 0)
1638 {
1639 type2_T *typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
1640
1641 // Check the types of the arguments.
1642 if (method_call && argoff > 1)
1643 {
1644 int i;
1645
1646 for (i = 0; i < argcount; ++i)
1647 shuffled_argtypes[i] = (i < argoff - 1)
1648 ? typep[i + 1]
1649 : (i == argoff - 1) ? typep[0] : typep[i];
1650 *argtypes = shuffled_argtypes;
1651 }
1652 else
1653 {
1654 int i;
1655
1656 for (i = 0; i < argcount; ++i)
1657 shuffled_argtypes[i] = typep[i];
1658 *argtypes = shuffled_argtypes;
1659 }
1660 if (internal_func_check_arg_types(*argtypes, func_idx, argcount,
1661 cctx) == FAIL)
1662 return FAIL;
1663 }
1664 return OK;
1665}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001666
1667/*
1668 * Generate an ISN_BCALL instruction.
1669 * "method_call" is TRUE for "value->method()"
1670 * Return FAIL if the number of arguments is wrong.
1671 */
1672 int
1673generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1674{
1675 isn_T *isn;
1676 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001677 type2_T *argtypes = NULL;
1678 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1679 type2_T *maptype = NULL;
1680 type_T *type;
Bram Moolenaar81330182022-02-01 12:11:58 +00001681 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001682
1683 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar16900322022-09-08 19:51:45 +01001684
1685 if (check_internal_func_args(cctx, func_idx, argcount, method_call,
1686 &argtypes, shuffled_argtypes) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001687 return FAIL;
1688
Bram Moolenaar16900322022-09-08 19:51:45 +01001689 if (internal_func_is_map(func_idx))
1690 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001691
1692 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1693 return FAIL;
1694 isn->isn_arg.bfunc.cbf_idx = func_idx;
1695 isn->isn_arg.bfunc.cbf_argcount = argcount;
1696
1697 // Drop the argument types and push the return type.
1698 stack->ga_len -= argcount;
Bram Moolenaar32517c42023-01-15 18:17:12 +00001699 type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type,
1700 cctx->ctx_type_list);
Bram Moolenaar81330182022-02-01 12:11:58 +00001701 if (push_type_stack2(cctx, type, decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001702 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001703
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001704 if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
1705 && maptype[0].type_decl->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001706 // Check that map() didn't change the item types.
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001707 generate_TYPECHECK(cctx, maptype[0].type_decl, FALSE, -1, FALSE, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001708
1709 return OK;
1710}
1711
1712/*
1713 * Generate an ISN_LISTAPPEND instruction. Works like add().
1714 * Argument count is already checked.
1715 */
1716 int
1717generate_LISTAPPEND(cctx_T *cctx)
1718{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001719 type_T *list_type;
1720 type_T *item_type;
1721 type_T *expected;
1722
1723 // Caller already checked that list_type is a list.
Bram Moolenaar81330182022-02-01 12:11:58 +00001724 // For checking the item type we use the declared type of the list and the
1725 // current type of the added item, adding a string to [1, 2] is OK.
1726 list_type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaarfa103972022-09-29 19:14:42 +01001727 if (arg_type_modifiable(list_type, 1) == FAIL)
1728 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001729 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001730 expected = list_type->tt_member;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001731 if (need_type(item_type, expected, FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001732 return FAIL;
1733
1734 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1735 return FAIL;
1736
Bram Moolenaar078a4612022-01-04 15:17:03 +00001737 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001738 return OK;
1739}
1740
1741/*
1742 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1743 * Argument count is already checked.
1744 */
1745 int
1746generate_BLOBAPPEND(cctx_T *cctx)
1747{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001748 type_T *item_type;
1749
Bram Moolenaarfa103972022-09-29 19:14:42 +01001750 // Caller already checked that blob_type is a blob, check it is modifiable.
1751 if (arg_type_modifiable(get_decl_type_on_stack(cctx, 1), 1) == FAIL)
1752 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001753 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001754 if (need_type(item_type, &t_number, FALSE,
1755 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001756 return FAIL;
1757
1758 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1759 return FAIL;
1760
Bram Moolenaar078a4612022-01-04 15:17:03 +00001761 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001762 return OK;
1763}
1764
1765/*
Bram Moolenaard0200c82023-01-28 15:19:40 +00001766 * Generate an ISN_DCALL, ISN_UCALL or ISN_METHODCALL instruction.
1767 * When calling a method on an object, of which we know the interface only,
1768 * then "cl" is the interface and "mi" the method index on the interface.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001769 * Return FAIL if the number of arguments is wrong.
1770 */
1771 int
Bram Moolenaard0200c82023-01-28 15:19:40 +00001772generate_CALL(
1773 cctx_T *cctx,
1774 ufunc_T *ufunc,
1775 class_T *cl,
1776 int mi,
1777 int pushed_argcount)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001778{
1779 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001780 int regular_args = ufunc->uf_args.ga_len;
1781 int argcount = pushed_argcount;
1782
1783 RETURN_OK_IF_SKIP(cctx);
1784 if (argcount > regular_args && !has_varargs(ufunc))
1785 {
1786 semsg(_(e_too_many_arguments_for_function_str),
1787 printable_func_name(ufunc));
1788 return FAIL;
1789 }
1790 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1791 {
1792 semsg(_(e_not_enough_arguments_for_function_str),
1793 printable_func_name(ufunc));
1794 return FAIL;
1795 }
1796
1797 if (ufunc->uf_def_status != UF_NOT_COMPILED
1798 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1799 {
1800 int i;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001801 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001802
1803 for (i = 0; i < argcount; ++i)
1804 {
1805 type_T *expected;
1806 type_T *actual;
1807
Bram Moolenaar078a4612022-01-04 15:17:03 +00001808 actual = get_type_on_stack(cctx, argcount - i - 1);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001809 if (actual->tt_type == VAR_SPECIAL
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001810 && i >= regular_args - ufunc->uf_def_args.ga_len)
1811 {
1812 // assume v:none used for default argument value
1813 continue;
1814 }
1815 if (i < regular_args)
1816 {
1817 if (ufunc->uf_arg_types == NULL)
1818 continue;
1819 expected = ufunc->uf_arg_types[i];
1820 }
1821 else if (ufunc->uf_va_type == NULL
1822 || ufunc->uf_va_type == &t_list_any)
1823 // possibly a lambda or "...: any"
1824 expected = &t_any;
1825 else
1826 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001827 if (need_type(actual, expected, FALSE,
1828 -argcount + i, i + 1, cctx, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001829 {
1830 arg_type_mismatch(expected, actual, i + 1);
1831 return FAIL;
1832 }
1833 }
Bram Moolenaar139575d2022-03-15 19:29:30 +00001834 compile_type = get_compile_type(ufunc);
1835 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001836 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaar139575d2022-03-15 19:29:30 +00001837 compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001838 return FAIL;
1839 }
1840 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1841 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001842 emsg_funcname(e_call_to_function_that_failed_to_compile_str,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001843 ufunc->uf_name);
1844 return FAIL;
1845 }
1846
Bram Moolenaard0200c82023-01-28 15:19:40 +00001847 if ((isn = generate_instr(cctx, cl != NULL ? ISN_METHODCALL
1848 : ufunc->uf_def_status != UF_NOT_COMPILED
1849 ? ISN_DCALL : ISN_UCALL)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001850 return FAIL;
Bram Moolenaar836137d2023-01-29 14:11:24 +00001851 if (cl != NULL /* isn->isn_type == ISN_METHODCALL */)
Bram Moolenaard0200c82023-01-28 15:19:40 +00001852 {
1853 isn->isn_arg.mfunc = ALLOC_ONE(cmfunc_T);
1854 if (isn->isn_arg.mfunc == NULL)
1855 return FAIL;
1856 isn->isn_arg.mfunc->cmf_itf = cl;
1857 ++cl->class_refcount;
1858 isn->isn_arg.mfunc->cmf_idx = mi;
1859 isn->isn_arg.mfunc->cmf_argcount = argcount;
1860 }
1861 else if (isn->isn_type == ISN_DCALL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001862 {
1863 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1864 isn->isn_arg.dfunc.cdf_argcount = argcount;
1865 }
1866 else
1867 {
1868 // A user function may be deleted and redefined later, can't use the
1869 // ufunc pointer, need to look it up again at runtime.
1870 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1871 isn->isn_arg.ufunc.cuf_argcount = argcount;
1872 }
1873
Bram Moolenaar078a4612022-01-04 15:17:03 +00001874 // drop the argument types
1875 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001876
Bram Moolenaar078a4612022-01-04 15:17:03 +00001877 // add return type
1878 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001879}
1880
1881/*
1882 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1883 */
1884 int
1885generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1886{
1887 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001888
1889 RETURN_OK_IF_SKIP(cctx);
1890 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1891 return FAIL;
1892 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1893 isn->isn_arg.ufunc.cuf_argcount = argcount;
1894
Bram Moolenaar078a4612022-01-04 15:17:03 +00001895 // drop the argument types
1896 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001897
Bram Moolenaar078a4612022-01-04 15:17:03 +00001898 // add return value
1899 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001900}
1901
1902/*
Bram Moolenaar16900322022-09-08 19:51:45 +01001903 * Check the arguments of function "type" against the types on the stack.
1904 * Returns OK or FAIL;
1905 */
1906 int
1907check_func_args_from_type(
1908 cctx_T *cctx,
1909 type_T *type,
1910 int argcount,
1911 int at_top,
1912 char_u *name)
1913{
1914 if (type->tt_argcount != -1)
1915 {
1916 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1917
1918 if (argcount < type->tt_min_argcount - varargs)
1919 {
1920 emsg_funcname(e_not_enough_arguments_for_function_str, name);
1921 return FAIL;
1922 }
1923 if (!varargs && argcount > type->tt_argcount)
1924 {
1925 emsg_funcname(e_too_many_arguments_for_function_str, name);
1926 return FAIL;
1927 }
1928 if (type->tt_args != NULL)
1929 {
1930 int i;
1931
1932 for (i = 0; i < argcount; ++i)
1933 {
1934 int offset = -argcount + i - (at_top ? 0 : 1);
1935 type_T *actual = get_type_on_stack(cctx, -1 - offset);
1936 type_T *expected;
1937
1938 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaar36818a92023-01-03 12:33:26 +00001939 {
1940 expected = type->tt_args[type->tt_argcount - 1];
1941 if (expected != NULL && expected->tt_type == VAR_LIST)
1942 expected = expected->tt_member;
1943 if (expected == NULL)
1944 expected = &t_any;
1945 }
Bram Moolenaar16900322022-09-08 19:51:45 +01001946 else if (i >= type->tt_min_argcount
1947 && actual->tt_type == VAR_SPECIAL)
1948 expected = &t_any;
1949 else
1950 expected = type->tt_args[i];
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001951 if (need_type(actual, expected, FALSE,
1952 offset, i + 1, cctx, TRUE, FALSE) == FAIL)
Bram Moolenaar16900322022-09-08 19:51:45 +01001953 {
1954 arg_type_mismatch(expected, actual, i + 1);
1955 return FAIL;
1956 }
1957 }
1958 }
1959 }
1960
1961 return OK;
1962}
1963/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001964 * Generate an ISN_PCALL instruction.
1965 * "type" is the type of the FuncRef.
1966 */
1967 int
1968generate_PCALL(
1969 cctx_T *cctx,
1970 int argcount,
1971 char_u *name,
1972 type_T *type,
1973 int at_top)
1974{
1975 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001976 type_T *ret_type;
1977
1978 RETURN_OK_IF_SKIP(cctx);
1979
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001980 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001981 ret_type = &t_any;
1982 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1983 {
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00001984 if (check_func_args_from_type(cctx, type, argcount, at_top, name)
1985 == FAIL)
Bram Moolenaar16900322022-09-08 19:51:45 +01001986 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001987
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001988 ret_type = type->tt_member;
1989 if (ret_type == &t_unknown)
1990 // return type not known yet, use a runtime check
1991 ret_type = &t_any;
1992 }
1993 else
1994 {
1995 semsg(_(e_not_callable_type_str), name);
1996 return FAIL;
1997 }
1998
1999 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2000 return FAIL;
2001 isn->isn_arg.pfunc.cpf_top = at_top;
2002 isn->isn_arg.pfunc.cpf_argcount = argcount;
2003
Bram Moolenaar078a4612022-01-04 15:17:03 +00002004 // drop the arguments and the funcref/partial
2005 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002006
Bram Moolenaar078a4612022-01-04 15:17:03 +00002007 // push the return value
2008 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002009
2010 // If partial is above the arguments it must be cleared and replaced with
2011 // the return value.
2012 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2013 return FAIL;
2014
2015 return OK;
2016}
2017
2018/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002019 * Generate an ISN_DEFER instruction.
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00002020 * "obj_method" is one for "obj.Method()", zero otherwise.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002021 */
2022 int
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00002023generate_DEFER(cctx_T *cctx, int var_idx, int obj_method, int argcount)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002024{
2025 isn_T *isn;
2026
2027 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00002028 if ((isn = generate_instr_drop(cctx,
2029 obj_method == 0 ? ISN_DEFER : ISN_DEFEROBJ,
2030 argcount + 1)) == NULL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002031 return FAIL;
2032 isn->isn_arg.defer.defer_var_idx = var_idx;
2033 isn->isn_arg.defer.defer_argcount = argcount;
2034 return OK;
2035}
2036
2037/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002038 * Generate an ISN_STRINGMEMBER instruction.
2039 */
2040 int
2041generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
2042{
2043 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002044 type_T *type;
2045
2046 RETURN_OK_IF_SKIP(cctx);
2047 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
2048 return FAIL;
2049 isn->isn_arg.string = vim_strnsave(name, len);
2050
2051 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00002052 type = get_type_on_stack(cctx, 0);
Bram Moolenaar0089ce22022-10-08 14:39:36 +01002053 if (type->tt_type != VAR_DICT
2054 && type->tt_type != VAR_ANY && type->tt_type != VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002055 {
2056 char *tofree;
2057
2058 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
2059 name, type_name(type, &tofree));
2060 vim_free(tofree);
2061 return FAIL;
2062 }
2063 // change dict type to dict member type
2064 if (type->tt_type == VAR_DICT)
2065 {
Bram Moolenaar0089ce22022-10-08 14:39:36 +01002066 type_T *ntype = type->tt_member->tt_type == VAR_UNKNOWN
Bram Moolenaar078a4612022-01-04 15:17:03 +00002067 ? &t_any : type->tt_member;
2068 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002069 }
2070
2071 return OK;
2072}
2073
2074/*
2075 * Generate an ISN_ECHO instruction.
2076 */
2077 int
2078generate_ECHO(cctx_T *cctx, int with_white, int count)
2079{
2080 isn_T *isn;
2081
2082 RETURN_OK_IF_SKIP(cctx);
2083 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2084 return FAIL;
2085 isn->isn_arg.echo.echo_with_white = with_white;
2086 isn->isn_arg.echo.echo_count = count;
2087
2088 return OK;
2089}
2090
2091/*
2092 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
2093 */
2094 int
2095generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
2096{
2097 isn_T *isn;
2098
Bram Moolenaar2eae3d22022-10-07 15:09:27 +01002099 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002100 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
2101 return FAIL;
2102 isn->isn_arg.number = count;
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002103 return OK;
2104}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002105
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002106/*
2107 * Generate an ISN_ECHOWINDOW instruction
2108 */
2109 int
2110generate_ECHOWINDOW(cctx_T *cctx, int count, long time)
2111{
2112 isn_T *isn;
2113
2114 if ((isn = generate_instr_drop(cctx, ISN_ECHOWINDOW, count)) == NULL)
2115 return FAIL;
2116 isn->isn_arg.echowin.ewin_count = count;
2117 isn->isn_arg.echowin.ewin_time = time;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002118 return OK;
2119}
2120
2121/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002122 * Generate an ISN_SOURCE instruction.
2123 */
2124 int
2125generate_SOURCE(cctx_T *cctx, int sid)
2126{
2127 isn_T *isn;
2128
2129 if ((isn = generate_instr(cctx, ISN_SOURCE)) == NULL)
2130 return FAIL;
2131 isn->isn_arg.number = sid;
2132
2133 return OK;
2134}
2135
2136/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002137 * Generate an ISN_PUT instruction.
2138 */
2139 int
2140generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2141{
2142 isn_T *isn;
2143
2144 RETURN_OK_IF_SKIP(cctx);
2145 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2146 return FAIL;
2147 isn->isn_arg.put.put_regname = regname;
2148 isn->isn_arg.put.put_lnum = lnum;
2149 return OK;
2150}
2151
2152/*
2153 * Generate an EXEC instruction that takes a string argument.
2154 * A copy is made of "line".
2155 */
2156 int
2157generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
2158{
2159 isn_T *isn;
2160
2161 RETURN_OK_IF_SKIP(cctx);
2162 if ((isn = generate_instr(cctx, isntype)) == NULL)
2163 return FAIL;
2164 isn->isn_arg.string = vim_strsave(line);
2165 return OK;
2166}
2167
2168/*
2169 * Generate an EXEC instruction that takes a string argument.
2170 * "str" must be allocated, it is consumed.
2171 */
2172 int
2173generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
2174{
2175 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002176 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002177
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002178 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002179 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002180 if ((isn = generate_instr(cctx, isntype)) == NULL)
2181 ret = FAIL;
2182 else
2183 {
2184 isn->isn_arg.string = str;
2185 return OK;
2186 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002187 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002188 vim_free(str);
2189 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002190}
2191
2192 int
2193generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2194{
2195 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002196
2197 RETURN_OK_IF_SKIP(cctx);
2198 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2199 return FAIL;
2200 isn->isn_arg.string = vim_strsave(line);
2201
Bram Moolenaar078a4612022-01-04 15:17:03 +00002202 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002203}
2204
2205 int
2206generate_EXECCONCAT(cctx_T *cctx, int count)
2207{
2208 isn_T *isn;
2209
2210 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2211 return FAIL;
2212 isn->isn_arg.number = count;
2213 return OK;
2214}
2215
2216/*
2217 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2218 */
2219 int
2220generate_RANGE(cctx_T *cctx, char_u *range)
2221{
2222 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002223
2224 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2225 return FAIL;
2226 isn->isn_arg.string = range;
2227
Bram Moolenaar078a4612022-01-04 15:17:03 +00002228 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002229}
2230
2231 int
2232generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2233{
2234 isn_T *isn;
2235
2236 RETURN_OK_IF_SKIP(cctx);
2237 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2238 return FAIL;
2239 isn->isn_arg.unpack.unp_count = var_count;
2240 isn->isn_arg.unpack.unp_semicolon = semicolon;
2241 return OK;
2242}
2243
2244/*
2245 * Generate an instruction for any command modifiers.
2246 */
2247 int
2248generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
2249{
2250 isn_T *isn;
2251
2252 if (has_cmdmod(cmod, FALSE))
2253 {
2254 cctx->ctx_has_cmdmod = TRUE;
2255
2256 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
2257 return FAIL;
2258 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2259 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2260 return FAIL;
2261 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
2262 // filter program now belongs to the instruction
2263 cmod->cmod_filter_regmatch.regprog = NULL;
2264 }
2265
2266 return OK;
2267}
2268
2269 int
2270generate_undo_cmdmods(cctx_T *cctx)
2271{
2272 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2273 return FAIL;
2274 cctx->ctx_has_cmdmod = FALSE;
2275 return OK;
2276}
2277
2278/*
2279 * Generate a STORE instruction for "dest", not being "dest_local".
Bram Moolenaard505d172022-12-18 21:42:55 +00002280 * "lhs" might be NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002281 * Return FAIL when out of memory.
2282 */
2283 int
2284generate_store_var(
2285 cctx_T *cctx,
2286 assign_dest_T dest,
2287 int opt_flags,
2288 int vimvaridx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002289 type_T *type,
Bram Moolenaard505d172022-12-18 21:42:55 +00002290 char_u *name,
2291 lhs_T *lhs)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002292{
2293 switch (dest)
2294 {
2295 case dest_option:
2296 return generate_STOREOPT(cctx, ISN_STOREOPT,
2297 skip_option_env_lead(name), opt_flags);
2298 case dest_func_option:
2299 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
2300 skip_option_env_lead(name), opt_flags);
2301 case dest_global:
2302 // include g: with the name, easier to execute that way
2303 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
2304 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
2305 case dest_buffer:
2306 // include b: with the name, easier to execute that way
2307 return generate_STORE(cctx, ISN_STOREB, 0, name);
2308 case dest_window:
2309 // include w: with the name, easier to execute that way
2310 return generate_STORE(cctx, ISN_STOREW, 0, name);
2311 case dest_tab:
2312 // include t: with the name, easier to execute that way
2313 return generate_STORE(cctx, ISN_STORET, 0, name);
2314 case dest_env:
2315 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
2316 case dest_reg:
2317 return generate_STORE(cctx, ISN_STOREREG,
2318 name[1] == '@' ? '"' : name[1], NULL);
2319 case dest_vimvar:
2320 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
2321 case dest_script:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002322 {
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002323 int scriptvar_idx = lhs->lhs_scriptvar_idx;
2324 int scriptvar_sid = lhs->lhs_scriptvar_sid;
2325 if (scriptvar_idx < 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002326 {
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002327 isntype_T isn_type = ISN_STORES;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002328
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002329 if (SCRIPT_ID_VALID(scriptvar_sid)
2330 && SCRIPT_ITEM(scriptvar_sid)->sn_import_autoload
2331 && SCRIPT_ITEM(scriptvar_sid)->sn_autoload_prefix
2332 == NULL)
2333 {
2334 // "import autoload './dir/script.vim'" - load script
2335 // first
2336 if (generate_SOURCE(cctx, scriptvar_sid) == FAIL)
2337 return FAIL;
2338 isn_type = ISN_STOREEXPORT;
2339 }
2340
2341 // "s:" may be included in the name.
2342 return generate_OLDSCRIPT(cctx, isn_type, name,
2343 scriptvar_sid, type);
2344 }
2345 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002346 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002347 }
Bram Moolenaard505d172022-12-18 21:42:55 +00002348 case dest_class_member:
2349 return generate_CLASSMEMBER(cctx, FALSE,
2350 lhs->lhs_class, lhs->lhs_classmember_idx);
2351
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002352 case dest_local:
2353 case dest_expr:
2354 // cannot happen
2355 break;
2356 }
2357 return FAIL;
2358}
2359
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002360/*
2361 * Return TRUE when inside a "for" or "while" loop.
2362 */
2363 int
2364inside_loop_scope(cctx_T *cctx)
2365{
2366 scope_T *scope = cctx->ctx_scope;
2367
2368 for (;;)
2369 {
2370 if (scope == NULL)
2371 break;
2372 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
2373 return TRUE;
2374 scope = scope->se_outer;
2375 }
2376 return FALSE;
2377}
2378
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002379 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002380generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002381{
2382 if (lhs->lhs_dest != dest_local)
2383 return generate_store_var(cctx, lhs->lhs_dest,
2384 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
Bram Moolenaard505d172022-12-18 21:42:55 +00002385 lhs->lhs_type, lhs->lhs_name, lhs);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002386
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002387 if (lhs->lhs_lvar == NULL)
2388 return OK;
2389
2390 garray_T *instr = &cctx->ctx_instr;
2391 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2392
2393 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
2394 // ISN_STORENR.
2395 // And "var = 0" does not need any instruction.
2396 if (lhs->lhs_lvar->lv_from_outer == 0
2397 && instr->ga_len == instr_count + 1
2398 && isn->isn_type == ISN_PUSHNR)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002399 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002400 varnumber_T val = isn->isn_arg.number;
2401 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002402
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002403 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002404 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002405 // zero is the default value, no need to do anything
2406 --instr->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002407 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002408 else
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002409 {
2410 isn->isn_type = ISN_STORENR;
2411 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
2412 isn->isn_arg.storenr.stnr_val = val;
2413 }
2414 if (stack->ga_len > 0)
2415 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002416 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002417 else if (lhs->lhs_lvar->lv_from_outer > 0)
2418 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
2419 lhs->lhs_lvar->lv_from_outer, lhs->lhs_lvar->lv_loop_idx);
2420 else
2421 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002422 return OK;
2423}
2424
2425#if defined(FEAT_PROFILE) || defined(PROTO)
2426 void
2427may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2428{
2429 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
2430 generate_instr(cctx, ISN_PROF_END);
2431}
2432#endif
2433
2434
2435/*
2436 * Delete an instruction, free what it contains.
2437 */
2438 void
2439delete_instr(isn_T *isn)
2440{
2441 switch (isn->isn_type)
2442 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002443 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002444 case ISN_DEF:
2445 case ISN_EXEC:
2446 case ISN_EXECRANGE:
2447 case ISN_EXEC_SPLIT:
2448 case ISN_LEGACY_EVAL:
2449 case ISN_LOADAUTO:
2450 case ISN_LOADB:
2451 case ISN_LOADENV:
2452 case ISN_LOADG:
2453 case ISN_LOADOPT:
2454 case ISN_LOADT:
2455 case ISN_LOADW:
2456 case ISN_LOCKUNLOCK:
2457 case ISN_PUSHEXC:
2458 case ISN_PUSHFUNC:
2459 case ISN_PUSHS:
2460 case ISN_RANGE:
2461 case ISN_STOREAUTO:
2462 case ISN_STOREB:
2463 case ISN_STOREENV:
2464 case ISN_STOREG:
2465 case ISN_STORET:
2466 case ISN_STOREW:
2467 case ISN_STRINGMEMBER:
2468 vim_free(isn->isn_arg.string);
2469 break;
2470
2471 case ISN_SUBSTITUTE:
2472 {
2473 int idx;
2474 isn_T *list = isn->isn_arg.subs.subs_instr;
2475
2476 vim_free(isn->isn_arg.subs.subs_cmd);
2477 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2478 delete_instr(list + idx);
2479 vim_free(list);
2480 }
2481 break;
2482
2483 case ISN_INSTR:
2484 {
2485 int idx;
2486 isn_T *list = isn->isn_arg.instr;
2487
2488 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2489 delete_instr(list + idx);
2490 vim_free(list);
2491 }
2492 break;
2493
2494 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002495 case ISN_LOADEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002496 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002497 case ISN_STOREEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002498 vim_free(isn->isn_arg.loadstore.ls_name);
2499 break;
2500
2501 case ISN_UNLET:
2502 case ISN_UNLETENV:
2503 vim_free(isn->isn_arg.unlet.ul_name);
2504 break;
2505
2506 case ISN_STOREOPT:
2507 case ISN_STOREFUNCOPT:
2508 vim_free(isn->isn_arg.storeopt.so_name);
2509 break;
2510
2511 case ISN_PUSHBLOB: // push blob isn_arg.blob
2512 blob_unref(isn->isn_arg.blob);
2513 break;
2514
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002515 case ISN_PUSHCLASS:
Bram Moolenaar30a84472023-02-27 08:07:14 +00002516 class_unref(isn->isn_arg.classarg);
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002517 break;
2518
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002519 case ISN_UCALL:
2520 vim_free(isn->isn_arg.ufunc.cuf_name);
2521 break;
2522
2523 case ISN_FUNCREF:
2524 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002525 funcref_T *funcref = &isn->isn_arg.funcref;
2526 funcref_extra_T *extra = funcref->fr_extra;
2527
2528 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002529 {
2530 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002531 + funcref->fr_dfunc_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002532 ufunc_T *ufunc = dfunc->df_ufunc;
2533
2534 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2535 func_ptr_unref(ufunc);
2536 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002537 if (extra != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002538 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002539 char_u *name = extra->fre_func_name;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002540
2541 if (name != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002542 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002543 func_unref(name);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002544 vim_free(name);
2545 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00002546 if (extra->fre_class != NULL)
2547 class_unref(extra->fre_class);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002548 vim_free(extra);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002549 }
2550 }
2551 break;
2552
2553 case ISN_DCALL:
2554 {
2555 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002556 + isn->isn_arg.dfunc.cdf_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002557
2558 if (dfunc->df_ufunc != NULL
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002559 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002560 func_ptr_unref(dfunc->df_ufunc);
2561 }
2562 break;
2563
Bram Moolenaard0200c82023-01-28 15:19:40 +00002564 case ISN_METHODCALL:
2565 {
2566 cmfunc_T *mfunc = isn->isn_arg.mfunc;
2567 class_unref(mfunc->cmf_itf);
2568 vim_free(mfunc);
2569 }
2570 break;
2571
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002572 case ISN_NEWFUNC:
2573 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002574 newfuncarg_T *arg = isn->isn_arg.newfunc.nf_arg;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002575
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002576 if (arg != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002577 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002578 ufunc_T *ufunc = find_func_even_dead(
2579 arg->nfa_lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002580
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002581 if (ufunc != NULL)
2582 {
2583 unlink_def_function(ufunc);
2584 func_ptr_unref(ufunc);
2585 }
2586
2587 vim_free(arg->nfa_lambda);
2588 vim_free(arg->nfa_global);
2589 vim_free(arg);
2590 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002591 }
2592 break;
2593
2594 case ISN_CHECKTYPE:
2595 case ISN_SETTYPE:
2596 free_type(isn->isn_arg.type.ct_type);
2597 break;
2598
2599 case ISN_CMDMOD:
2600 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002601 ->cmod_filter_regmatch.regprog);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002602 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2603 break;
2604
2605 case ISN_LOADSCRIPT:
2606 case ISN_STORESCRIPT:
2607 vim_free(isn->isn_arg.script.scriptref);
2608 break;
2609
Bram Moolenaard505d172022-12-18 21:42:55 +00002610 case ISN_LOAD_CLASSMEMBER:
2611 case ISN_STORE_CLASSMEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00002612 case ISN_GET_ITF_MEMBER:
Bram Moolenaard505d172022-12-18 21:42:55 +00002613 class_unref(isn->isn_arg.classmember.cm_class);
2614 break;
2615
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002616 case ISN_STOREINDEX:
2617 class_unref(isn->isn_arg.storeindex.si_class);
2618 break;
2619
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002620 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002621 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002622 break;
2623
2624 case ISN_CEXPR_CORE:
2625 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2626 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2627 break;
2628
2629 case ISN_2BOOL:
2630 case ISN_2STRING:
2631 case ISN_2STRING_ANY:
2632 case ISN_ADDBLOB:
2633 case ISN_ADDLIST:
2634 case ISN_ANYINDEX:
2635 case ISN_ANYSLICE:
2636 case ISN_BCALL:
2637 case ISN_BLOBAPPEND:
2638 case ISN_BLOBINDEX:
2639 case ISN_BLOBSLICE:
2640 case ISN_CATCH:
2641 case ISN_CEXPR_AUCMD:
2642 case ISN_CHECKLEN:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002643 case ISN_CLEARDICT:
2644 case ISN_CMDMOD_REV:
2645 case ISN_COMPAREANY:
2646 case ISN_COMPAREBLOB:
2647 case ISN_COMPAREBOOL:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002648 case ISN_COMPARECLASS:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002649 case ISN_COMPAREDICT:
2650 case ISN_COMPAREFLOAT:
2651 case ISN_COMPAREFUNC:
2652 case ISN_COMPARELIST:
2653 case ISN_COMPARENR:
Bram Moolenaar7a222242022-03-01 19:23:24 +00002654 case ISN_COMPARENULL:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002655 case ISN_COMPAREOBJECT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002656 case ISN_COMPARESPECIAL:
2657 case ISN_COMPARESTRING:
2658 case ISN_CONCAT:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002659 case ISN_CONSTRUCT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002660 case ISN_COND2BOOL:
2661 case ISN_DEBUG:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002662 case ISN_DEFER:
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00002663 case ISN_DEFEROBJ:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002664 case ISN_DROP:
2665 case ISN_ECHO:
2666 case ISN_ECHOCONSOLE:
2667 case ISN_ECHOERR:
2668 case ISN_ECHOMSG:
Bram Moolenaar68a635a2022-09-01 17:26:17 +01002669 case ISN_ECHOWINDOW:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002670 case ISN_ENDLOOP:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002671 case ISN_ENDTRY:
2672 case ISN_EXECCONCAT:
2673 case ISN_EXECUTE:
2674 case ISN_FINALLY:
2675 case ISN_FINISH:
2676 case ISN_FOR:
2677 case ISN_GETITEM:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002678 case ISN_GET_OBJ_MEMBER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002679 case ISN_JUMP:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002680 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002681 case ISN_JUMP_IF_ARG_SET:
2682 case ISN_LISTAPPEND:
2683 case ISN_LISTINDEX:
2684 case ISN_LISTSLICE:
2685 case ISN_LOAD:
2686 case ISN_LOADBDICT:
2687 case ISN_LOADGDICT:
2688 case ISN_LOADOUTER:
2689 case ISN_LOADREG:
2690 case ISN_LOADTDICT:
2691 case ISN_LOADV:
2692 case ISN_LOADWDICT:
2693 case ISN_LOCKCONST:
2694 case ISN_MEMBER:
2695 case ISN_NEGATENR:
2696 case ISN_NEWDICT:
2697 case ISN_NEWLIST:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002698 case ISN_NEWPARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002699 case ISN_OPANY:
2700 case ISN_OPFLOAT:
2701 case ISN_OPNR:
2702 case ISN_PCALL:
2703 case ISN_PCALL_END:
2704 case ISN_PROF_END:
2705 case ISN_PROF_START:
2706 case ISN_PUSHBOOL:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002707 case ISN_PUSHCHANNEL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002708 case ISN_PUSHF:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002709 case ISN_PUSHJOB:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002710 case ISN_PUSHNR:
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002711 case ISN_PUSHOBJ:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002712 case ISN_PUSHSPEC:
2713 case ISN_PUT:
2714 case ISN_REDIREND:
2715 case ISN_REDIRSTART:
2716 case ISN_RETURN:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002717 case ISN_RETURN_OBJECT:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002718 case ISN_RETURN_VOID:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002719 case ISN_SHUFFLE:
2720 case ISN_SLICE:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002721 case ISN_SOURCE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002722 case ISN_STORE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002723 case ISN_STORENR:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002724 case ISN_STOREOUTER:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002725 case ISN_STORE_THIS:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002726 case ISN_STORERANGE:
2727 case ISN_STOREREG:
2728 case ISN_STOREV:
2729 case ISN_STRINDEX:
2730 case ISN_STRSLICE:
2731 case ISN_THROW:
2732 case ISN_TRYCONT:
2733 case ISN_UNLETINDEX:
2734 case ISN_UNLETRANGE:
2735 case ISN_UNPACK:
2736 case ISN_USEDICT:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002737 case ISN_WHILE:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002738 // nothing allocated
2739 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002740 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002741}
2742
2743 void
2744clear_instr_ga(garray_T *gap)
2745{
2746 int idx;
2747
2748 for (idx = 0; idx < gap->ga_len; ++idx)
2749 delete_instr(((isn_T *)gap->ga_data) + idx);
2750 ga_clear(gap);
2751}
2752
2753
2754#endif // defined(FEAT_EVAL)