blob: 3a8d695dad322de6b2ddb2566852d5c9abc4742f [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/*
117 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
118 * But only for simple types.
119 * When "tolerant" is TRUE convert most types to string, e.g. a List.
120 */
121 int
122may_generate_2STRING(int offset, int tolerant, cctx_T *cctx)
123{
124 isn_T *isn;
125 isntype_T isntype = ISN_2STRING;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000126 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000127
128 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +0000129 type = get_type_on_stack(cctx, -1 - offset);
130 switch (type->tt_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000131 {
132 // nothing to be done
133 case VAR_STRING: return OK;
134
135 // conversion possible
136 case VAR_SPECIAL:
137 case VAR_BOOL:
138 case VAR_NUMBER:
139 case VAR_FLOAT:
140 break;
141
142 // conversion possible (with runtime check)
143 case VAR_ANY:
144 case VAR_UNKNOWN:
145 isntype = ISN_2STRING_ANY;
146 break;
147
148 // conversion possible when tolerant
149 case VAR_LIST:
150 if (tolerant)
151 {
152 isntype = ISN_2STRING_ANY;
153 break;
154 }
155 // FALLTHROUGH
156
157 // conversion not possible
158 case VAR_VOID:
159 case VAR_BLOB:
160 case VAR_FUNC:
161 case VAR_PARTIAL:
162 case VAR_DICT:
163 case VAR_JOB:
164 case VAR_CHANNEL:
165 case VAR_INSTR:
Bram Moolenaar078a4612022-01-04 15:17:03 +0000166 to_string_error(type->tt_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000167 return FAIL;
168 }
169
Bram Moolenaar078a4612022-01-04 15:17:03 +0000170 set_type_on_stack(cctx, &t_string, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000171 if ((isn = generate_instr(cctx, isntype)) == NULL)
172 return FAIL;
173 isn->isn_arg.tostring.offset = offset;
174 isn->isn_arg.tostring.tolerant = tolerant;
175
176 return OK;
177}
178
179 static int
180check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
181{
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000182 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT
183 || type1 == VAR_ANY || type1 == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000184 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000185 || type2 == VAR_ANY || type2 == VAR_UNKNOWN)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000186 {
187 if (*op == '+')
188 emsg(_(e_wrong_argument_type_for_plus));
189 else
190 semsg(_(e_char_requires_number_or_float_arguments), *op);
191 return FAIL;
192 }
193 return OK;
194}
195
196/*
197 * Generate instruction for "+". For a list this creates a new list.
198 */
199 int
200generate_add_instr(
201 cctx_T *cctx,
202 vartype_T vartype,
203 type_T *type1,
204 type_T *type2,
205 exprtype_T expr_type)
206{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000207 isn_T *isn = generate_instr_drop(cctx,
208 vartype == VAR_NUMBER ? ISN_OPNR
209 : vartype == VAR_LIST ? ISN_ADDLIST
210 : vartype == VAR_BLOB ? ISN_ADDBLOB
211#ifdef FEAT_FLOAT
212 : vartype == VAR_FLOAT ? ISN_OPFLOAT
213#endif
214 : ISN_OPANY, 1);
215
216 if (vartype != VAR_LIST && vartype != VAR_BLOB
217 && type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000218 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000219 && type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000220 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000221 && check_number_or_float(
222 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
223 return FAIL;
224
225 if (isn != NULL)
226 {
227 if (isn->isn_type == ISN_ADDLIST)
228 isn->isn_arg.op.op_type = expr_type;
229 else
230 isn->isn_arg.op.op_type = EXPR_ADD;
231 }
232
233 // When concatenating two lists with different member types the member type
234 // becomes "any".
235 if (vartype == VAR_LIST
236 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
237 && type1->tt_member != type2->tt_member)
Bram Moolenaar078a4612022-01-04 15:17:03 +0000238 set_type_on_stack(cctx, &t_list_any, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000239
240 return isn == NULL ? FAIL : OK;
241}
242
243/*
244 * Get the type to use for an instruction for an operation on "type1" and
245 * "type2". If they are matching use a type-specific instruction. Otherwise
246 * fall back to runtime type checking.
247 */
248 vartype_T
249operator_type(type_T *type1, type_T *type2)
250{
251 if (type1->tt_type == type2->tt_type
252 && (type1->tt_type == VAR_NUMBER
253 || type1->tt_type == VAR_LIST
254#ifdef FEAT_FLOAT
255 || type1->tt_type == VAR_FLOAT
256#endif
257 || type1->tt_type == VAR_BLOB))
258 return type1->tt_type;
259 return VAR_ANY;
260}
261
262/*
263 * Generate an instruction with two arguments. The instruction depends on the
264 * type of the arguments.
265 */
266 int
267generate_two_op(cctx_T *cctx, char_u *op)
268{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000269 type_T *type1;
270 type_T *type2;
271 vartype_T vartype;
272 isn_T *isn;
273
274 RETURN_OK_IF_SKIP(cctx);
275
276 // Get the known type of the two items on the stack.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000277 type1 = get_type_on_stack(cctx, 1);
278 type2 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000279 vartype = operator_type(type1, type2);
280
281 switch (*op)
282 {
283 case '+':
284 if (generate_add_instr(cctx, vartype, type1, type2,
285 EXPR_COPY) == FAIL)
286 return FAIL;
287 break;
288
289 case '-':
290 case '*':
291 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
292 op) == FAIL)
293 return FAIL;
294 if (vartype == VAR_NUMBER)
295 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
296#ifdef FEAT_FLOAT
297 else if (vartype == VAR_FLOAT)
298 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
299#endif
300 else
301 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
302 if (isn != NULL)
303 isn->isn_arg.op.op_type = *op == '*'
304 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
305 break;
306
307 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000308 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000309 && type1->tt_type != VAR_NUMBER)
310 || (type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000311 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000312 && type2->tt_type != VAR_NUMBER))
313 {
314 emsg(_(e_percent_requires_number_arguments));
315 return FAIL;
316 }
317 isn = generate_instr_drop(cctx,
318 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
319 if (isn != NULL)
320 isn->isn_arg.op.op_type = EXPR_REM;
321 break;
322 }
323
324 // correct type of result
325 if (vartype == VAR_ANY)
326 {
327 type_T *type = &t_any;
328
329#ifdef FEAT_FLOAT
330 // float+number and number+float results in float
331 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
332 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
333 type = &t_float;
334#endif
Bram Moolenaar078a4612022-01-04 15:17:03 +0000335 set_type_on_stack(cctx, type, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000336 }
337
338 return OK;
339}
340
341/*
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000342 * Get the instruction to use for comparing two values with specified types.
343 * Either "tv1" and "tv2" are passed or "type1" and "type2".
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000344 * Return ISN_DROP when failed.
345 */
346 static isntype_T
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000347get_compare_isn(
348 exprtype_T exprtype,
349 typval_T *tv1,
350 typval_T *tv2,
351 type_T *type1,
352 type_T *type2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000353{
354 isntype_T isntype = ISN_DROP;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000355 vartype_T vartype1 = tv1 != NULL ? tv1->v_type : type1->tt_type;
356 vartype_T vartype2 = tv2 != NULL ? tv2->v_type : type2->tt_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000357
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000358 if (vartype1 == VAR_UNKNOWN)
359 vartype1 = VAR_ANY;
360 if (vartype2 == VAR_UNKNOWN)
361 vartype2 = VAR_ANY;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000362
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000363 if (vartype1 == vartype2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000364 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000365 switch (vartype1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000366 {
367 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
368 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
369 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
370 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
371 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
372 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
373 case VAR_LIST: isntype = ISN_COMPARELIST; break;
374 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
375 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
376 default: isntype = ISN_COMPAREANY; break;
377 }
378 }
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000379 else if (vartype1 == VAR_ANY || vartype2 == VAR_ANY
380 || ((vartype1 == VAR_NUMBER || vartype1 == VAR_FLOAT)
381 && (vartype2 == VAR_NUMBER || vartype2 == VAR_FLOAT))
382 || (vartype1 == VAR_FUNC && vartype2 == VAR_PARTIAL)
383 || (vartype1 == VAR_PARTIAL && vartype2 == VAR_FUNC))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000384 isntype = ISN_COMPAREANY;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000385 else if (vartype1 == VAR_SPECIAL || vartype2 == VAR_SPECIAL)
Bram Moolenaar7a222242022-03-01 19:23:24 +0000386 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000387 if ((vartype1 == VAR_SPECIAL
388 && (tv1 != NULL ? tv1->vval.v_number == VVAL_NONE
389 : type1 == &t_none)
390 && vartype2 != VAR_STRING)
391 || (vartype2 == VAR_SPECIAL
392 && (tv2 != NULL ? tv2->vval.v_number == VVAL_NONE
393 : type2 == &t_none)
394 && vartype1 != VAR_STRING))
395 {
396 semsg(_(e_cannot_compare_str_with_str),
397 vartype_name(vartype1), vartype_name(vartype2));
398 return ISN_DROP;
399 }
Bram Moolenaar05667812022-03-15 20:21:33 +0000400 // although comparing null with number, float or bool is not useful, we
401 // allow it
Bram Moolenaar7a222242022-03-01 19:23:24 +0000402 isntype = ISN_COMPARENULL;
403 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000404
405 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
406 && (isntype == ISN_COMPAREBOOL
407 || isntype == ISN_COMPARESPECIAL
408 || isntype == ISN_COMPARENR
409 || isntype == ISN_COMPAREFLOAT))
410 {
411 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000412 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(vartype1));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000413 return ISN_DROP;
414 }
415 if (isntype == ISN_DROP
416 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000417 && (vartype1 == VAR_BOOL || vartype1 == VAR_SPECIAL
418 || vartype2 == VAR_BOOL || vartype2 == VAR_SPECIAL)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000419 || ((exprtype != EXPR_EQUAL && exprtype != EXPR_NEQUAL
Bram Moolenaar7a222242022-03-01 19:23:24 +0000420 && exprtype != EXPR_IS && exprtype != EXPR_ISNOT
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000421 && (vartype1 == VAR_BLOB || vartype2 == VAR_BLOB
422 || vartype1 == VAR_LIST || vartype2 == VAR_LIST))))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000423 {
424 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000425 vartype_name(vartype1), vartype_name(vartype2));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000426 return ISN_DROP;
427 }
428 return isntype;
429}
430
431 int
432check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
433{
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000434 if (get_compare_isn(type, tv1, tv2, NULL, NULL) == ISN_DROP)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000435 return FAIL;
436 return OK;
437}
438
439/*
440 * Generate an ISN_COMPARE* instruction with a boolean result.
441 */
442 int
443generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
444{
445 isntype_T isntype;
446 isn_T *isn;
447 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000448
449 RETURN_OK_IF_SKIP(cctx);
450
451 // Get the known type of the two items on the stack. If they are matching
452 // use a type-specific instruction. Otherwise fall back to runtime type
453 // checking.
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000454 isntype = get_compare_isn(exprtype, NULL, NULL,
455 get_type_on_stack(cctx, 1), get_type_on_stack(cctx, 0));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000456 if (isntype == ISN_DROP)
457 return FAIL;
458
459 if ((isn = generate_instr(cctx, isntype)) == NULL)
460 return FAIL;
461 isn->isn_arg.op.op_type = exprtype;
462 isn->isn_arg.op.op_ic = ic;
463
464 // takes two arguments, puts one bool back
465 if (stack->ga_len >= 2)
466 {
467 --stack->ga_len;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000468 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000469 }
470
471 return OK;
472}
473
474/*
LemonBoy372bcce2022-04-25 12:43:20 +0100475 * Generate an ISN_CONCAT instruction.
476 * "count" is the number of stack elements to join together and it must be
477 * greater or equal to one.
478 * The caller ensures all the "count" elements on the stack have the right type.
479 */
480 int
481generate_CONCAT(cctx_T *cctx, int count)
482{
483 isn_T *isn;
484 garray_T *stack = &cctx->ctx_type_stack;
485
486 RETURN_OK_IF_SKIP(cctx);
487
488 if (count < 1)
489 return FAIL;
490
491 if ((isn = generate_instr(cctx, ISN_CONCAT)) == NULL)
492 return FAIL;
493 isn->isn_arg.number = count;
494
495 // drop the argument types
496 stack->ga_len -= count - 1;
497
498 return OK;
499}
500
501/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000502 * Generate an ISN_2BOOL instruction.
503 * "offset" is the offset in the type stack.
504 */
505 int
506generate_2BOOL(cctx_T *cctx, int invert, int offset)
507{
508 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000509
510 RETURN_OK_IF_SKIP(cctx);
511 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
512 return FAIL;
513 isn->isn_arg.tobool.invert = invert;
514 isn->isn_arg.tobool.offset = offset;
515
516 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000517 set_type_on_stack(cctx, &t_bool, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000518
519 return OK;
520}
521
522/*
523 * Generate an ISN_COND2BOOL instruction.
524 */
525 int
526generate_COND2BOOL(cctx_T *cctx)
527{
528 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000529
530 RETURN_OK_IF_SKIP(cctx);
531 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
532 return FAIL;
533
534 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000535 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000536
537 return OK;
538}
539
540 int
541generate_TYPECHECK(
542 cctx_T *cctx,
543 type_T *expected,
544 int offset,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100545 int is_var,
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000546 int argidx)
547{
548 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000549
550 RETURN_OK_IF_SKIP(cctx);
551 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
552 return FAIL;
553 isn->isn_arg.type.ct_type = alloc_type(expected);
554 isn->isn_arg.type.ct_off = (int8_T)offset;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100555 isn->isn_arg.type.ct_is_var = is_var;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000556 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
557
558 // type becomes expected
Bram Moolenaar078a4612022-01-04 15:17:03 +0000559 set_type_on_stack(cctx, expected, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000560
561 return OK;
562}
563
564 int
565generate_SETTYPE(
566 cctx_T *cctx,
567 type_T *expected)
568{
569 isn_T *isn;
570
571 RETURN_OK_IF_SKIP(cctx);
572 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
573 return FAIL;
574 isn->isn_arg.type.ct_type = alloc_type(expected);
575 return OK;
576}
577
578/*
579 * Generate a PUSH instruction for "tv".
580 * "tv" will be consumed or cleared.
581 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
582 */
583 int
584generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
585{
586 if (tv != NULL)
587 {
588 switch (tv->v_type)
589 {
590 case VAR_UNKNOWN:
591 break;
592 case VAR_BOOL:
593 generate_PUSHBOOL(cctx, tv->vval.v_number);
594 break;
595 case VAR_SPECIAL:
596 generate_PUSHSPEC(cctx, tv->vval.v_number);
597 break;
598 case VAR_NUMBER:
599 generate_PUSHNR(cctx, tv->vval.v_number);
600 break;
601#ifdef FEAT_FLOAT
602 case VAR_FLOAT:
603 generate_PUSHF(cctx, tv->vval.v_float);
604 break;
605#endif
606 case VAR_BLOB:
607 generate_PUSHBLOB(cctx, tv->vval.v_blob);
608 tv->vval.v_blob = NULL;
609 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000610 case VAR_LIST:
611 if (tv->vval.v_list != NULL)
612 iemsg("non-empty list constant not supported");
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100613 generate_NEWLIST(cctx, 0, TRUE);
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000614 break;
615 case VAR_DICT:
616 if (tv->vval.v_dict != NULL)
617 iemsg("non-empty dict constant not supported");
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100618 generate_NEWDICT(cctx, 0, TRUE);
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000619 break;
620#ifdef FEAT_JOB_CHANNEL
621 case VAR_JOB:
622 if (tv->vval.v_job != NULL)
623 iemsg("non-null job constant not supported");
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000624 generate_PUSHJOB(cctx);
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000625 break;
626 case VAR_CHANNEL:
627 if (tv->vval.v_channel != NULL)
628 iemsg("non-null channel constant not supported");
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000629 generate_PUSHCHANNEL(cctx);
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000630 break;
631#endif
632 case VAR_FUNC:
633 if (tv->vval.v_string != NULL)
634 iemsg("non-null function constant not supported");
635 generate_PUSHFUNC(cctx, NULL, &t_func_unknown);
636 break;
637 case VAR_PARTIAL:
638 if (tv->vval.v_partial != NULL)
639 iemsg("non-null partial constant not supported");
640 if (generate_instr_type(cctx, ISN_NEWPARTIAL, &t_func_unknown)
641 == NULL)
642 return FAIL;
643 break;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000644 case VAR_STRING:
645 generate_PUSHS(cctx, &tv->vval.v_string);
646 tv->vval.v_string = NULL;
647 break;
648 default:
649 iemsg("constant type not supported");
650 clear_tv(tv);
651 return FAIL;
652 }
653 tv->v_type = VAR_UNKNOWN;
654 }
655 return OK;
656}
657
658/*
659 * Generate an ISN_PUSHNR instruction.
660 */
661 int
662generate_PUSHNR(cctx_T *cctx, varnumber_T number)
663{
664 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000665
666 RETURN_OK_IF_SKIP(cctx);
667 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
668 return FAIL;
669 isn->isn_arg.number = number;
670
671 if (number == 0 || number == 1)
672 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000673 set_type_on_stack(cctx, &t_number_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000674 return OK;
675}
676
677/*
678 * Generate an ISN_PUSHBOOL instruction.
679 */
680 int
681generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
682{
683 isn_T *isn;
684
685 RETURN_OK_IF_SKIP(cctx);
686 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
687 return FAIL;
688 isn->isn_arg.number = number;
689
690 return OK;
691}
692
693/*
694 * Generate an ISN_PUSHSPEC instruction.
695 */
696 int
697generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
698{
699 isn_T *isn;
700
701 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000702 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC,
703 number == VVAL_NULL ? &t_null : &t_none)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000704 return FAIL;
705 isn->isn_arg.number = number;
706
707 return OK;
708}
709
710#if defined(FEAT_FLOAT) || defined(PROTO)
711/*
712 * Generate an ISN_PUSHF instruction.
713 */
714 int
715generate_PUSHF(cctx_T *cctx, float_T fnumber)
716{
717 isn_T *isn;
718
719 RETURN_OK_IF_SKIP(cctx);
720 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
721 return FAIL;
722 isn->isn_arg.fnumber = fnumber;
723
724 return OK;
725}
726#endif
727
728/*
729 * Generate an ISN_PUSHS instruction.
730 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100731 * Note that if "str" is used in the instruction OK is returned and "*str" is
732 * not set to NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000733 */
734 int
735generate_PUSHS(cctx_T *cctx, char_u **str)
736{
737 isn_T *isn;
738
739 if (cctx->ctx_skip == SKIP_YES)
740 {
741 if (str != NULL)
742 VIM_CLEAR(*str);
743 return OK;
744 }
745 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
746 {
747 if (str != NULL)
748 VIM_CLEAR(*str);
749 return FAIL;
750 }
751 isn->isn_arg.string = str == NULL ? NULL : *str;
752
753 return OK;
754}
755
756/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000757 * Generate an ISN_PUSHCHANNEL instruction. Channel is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000758 */
759 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000760generate_PUSHCHANNEL(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000761{
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100762#ifdef FEAT_JOB_CHANNEL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000763 isn_T *isn;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100764#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000765
766 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100767#ifdef FEAT_JOB_CHANNEL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000768 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
769 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000770 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100771#else
772 emsg(_(e_channel_job_feature_not_available));
773 return FAIL;
774#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000775}
776
777/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000778 * Generate an ISN_PUSHJOB instruction. Job is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000779 */
780 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000781generate_PUSHJOB(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000782{
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100783#ifdef FEAT_JOB_CHANNEL
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000784 isn_T *isn;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100785#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000786
787 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100788#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000789 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_job)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000790 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000791 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100792#else
793 emsg(_(e_channel_job_feature_not_available));
794 return FAIL;
795#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000796}
797
798/*
799 * Generate an ISN_PUSHBLOB instruction.
800 * Consumes "blob".
801 */
802 int
803generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
804{
805 isn_T *isn;
806
807 RETURN_OK_IF_SKIP(cctx);
808 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
809 return FAIL;
810 isn->isn_arg.blob = blob;
811
812 return OK;
813}
814
815/*
816 * Generate an ISN_PUSHFUNC instruction with name "name".
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000817 */
818 int
819generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
820{
821 isn_T *isn;
822 char_u *funcname;
823
824 RETURN_OK_IF_SKIP(cctx);
825 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
826 return FAIL;
827 if (name == NULL)
828 funcname = NULL;
Bram Moolenaard041f422022-01-12 19:54:00 +0000829 else if (*name == K_SPECIAL // script-local
830 || vim_strchr(name, AUTOLOAD_CHAR) != NULL) // autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000831 funcname = vim_strsave(name);
832 else
833 {
834 funcname = alloc(STRLEN(name) + 3);
835 if (funcname != NULL)
836 {
837 STRCPY(funcname, "g:");
838 STRCPY(funcname + 2, name);
839 }
840 }
841
842 isn->isn_arg.string = funcname;
843 return OK;
844}
845
846/*
Bram Moolenaar06b77222022-01-25 15:51:56 +0000847 * Generate an ISN_AUTOLOAD instruction.
848 */
849 int
850generate_AUTOLOAD(cctx_T *cctx, char_u *name, type_T *type)
851{
852 isn_T *isn;
853
854 RETURN_OK_IF_SKIP(cctx);
855 if ((isn = generate_instr_type(cctx, ISN_AUTOLOAD, type)) == NULL)
856 return FAIL;
857 isn->isn_arg.string = vim_strsave(name);
858 if (isn->isn_arg.string == NULL)
859 return FAIL;
860 return OK;
861}
862
863/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000864 * Generate an ISN_GETITEM instruction with "index".
865 * "with_op" is TRUE for "+=" and other operators, the stack has the current
866 * value below the list with values.
867 */
868 int
869generate_GETITEM(cctx_T *cctx, int index, int with_op)
870{
871 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000872 type_T *type = get_type_on_stack(cctx, with_op ? 1 : 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000873 type_T *item_type = &t_any;
874
875 RETURN_OK_IF_SKIP(cctx);
876
877 if (type->tt_type != VAR_LIST)
878 {
879 // cannot happen, caller has checked the type
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000880 emsg(_(e_list_required));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000881 return FAIL;
882 }
883 item_type = type->tt_member;
884 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
885 return FAIL;
886 isn->isn_arg.getitem.gi_index = index;
887 isn->isn_arg.getitem.gi_with_op = with_op;
888
889 // add the item type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +0000890 return push_type_stack(cctx, item_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000891}
892
893/*
894 * Generate an ISN_SLICE instruction with "count".
895 */
896 int
897generate_SLICE(cctx_T *cctx, int count)
898{
899 isn_T *isn;
900
901 RETURN_OK_IF_SKIP(cctx);
902 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
903 return FAIL;
904 isn->isn_arg.number = count;
905 return OK;
906}
907
908/*
909 * Generate an ISN_CHECKLEN instruction with "min_len".
910 */
911 int
912generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
913{
914 isn_T *isn;
915
916 RETURN_OK_IF_SKIP(cctx);
917
918 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
919 return FAIL;
920 isn->isn_arg.checklen.cl_min_len = min_len;
921 isn->isn_arg.checklen.cl_more_OK = more_OK;
922
923 return OK;
924}
925
926/*
927 * Generate an ISN_STORE instruction.
928 */
929 int
930generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
931{
932 isn_T *isn;
933
934 RETURN_OK_IF_SKIP(cctx);
935 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
936 return FAIL;
937 if (name != NULL)
938 isn->isn_arg.string = vim_strsave(name);
939 else
940 isn->isn_arg.number = idx;
941
942 return OK;
943}
944
945/*
946 * Generate an ISN_STOREOUTER instruction.
947 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000948 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000949generate_STOREOUTER(cctx_T *cctx, int idx, int level)
950{
951 isn_T *isn;
952
953 RETURN_OK_IF_SKIP(cctx);
954 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
955 return FAIL;
956 isn->isn_arg.outer.outer_idx = idx;
957 isn->isn_arg.outer.outer_depth = level;
958
959 return OK;
960}
961
962/*
963 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
964 */
965 int
966generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
967{
968 isn_T *isn;
969
970 RETURN_OK_IF_SKIP(cctx);
971 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
972 return FAIL;
973 isn->isn_arg.storenr.stnr_idx = idx;
974 isn->isn_arg.storenr.stnr_val = value;
975
976 return OK;
977}
978
979/*
980 * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction
981 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000982 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000983generate_STOREOPT(
984 cctx_T *cctx,
985 isntype_T isn_type,
986 char_u *name,
987 int opt_flags)
988{
989 isn_T *isn;
990
991 RETURN_OK_IF_SKIP(cctx);
992 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
993 return FAIL;
994 isn->isn_arg.storeopt.so_name = vim_strsave(name);
995 isn->isn_arg.storeopt.so_flags = opt_flags;
996
997 return OK;
998}
999
1000/*
1001 * Generate an ISN_LOAD or similar instruction.
1002 */
1003 int
1004generate_LOAD(
1005 cctx_T *cctx,
1006 isntype_T isn_type,
1007 int idx,
1008 char_u *name,
1009 type_T *type)
1010{
1011 isn_T *isn;
1012
1013 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001014 if ((isn = generate_instr_type2(cctx, isn_type, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001015 return FAIL;
1016 if (name != NULL)
1017 isn->isn_arg.string = vim_strsave(name);
1018 else
1019 isn->isn_arg.number = idx;
1020
1021 return OK;
1022}
1023
1024/*
1025 * Generate an ISN_LOADOUTER instruction
1026 */
1027 int
1028generate_LOADOUTER(
1029 cctx_T *cctx,
1030 int idx,
1031 int nesting,
1032 type_T *type)
1033{
1034 isn_T *isn;
1035
1036 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001037 if ((isn = generate_instr_type2(cctx, ISN_LOADOUTER, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001038 return FAIL;
1039 isn->isn_arg.outer.outer_idx = idx;
1040 isn->isn_arg.outer.outer_depth = nesting;
1041
1042 return OK;
1043}
1044
1045/*
1046 * Generate an ISN_LOADV instruction for v:var.
1047 */
1048 int
1049generate_LOADV(
1050 cctx_T *cctx,
1051 char_u *name,
1052 int error)
1053{
1054 int di_flags;
1055 int vidx = find_vim_var(name, &di_flags);
1056 type_T *type;
1057
1058 RETURN_OK_IF_SKIP(cctx);
1059 if (vidx < 0)
1060 {
1061 if (error)
1062 semsg(_(e_variable_not_found_str), name);
1063 return FAIL;
1064 }
Bram Moolenaard787e402021-12-24 21:36:12 +00001065 type = get_vim_var_type(vidx, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001066 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
1067}
1068
1069/*
1070 * Generate an ISN_UNLET instruction.
1071 */
1072 int
1073generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
1074{
1075 isn_T *isn;
1076
1077 RETURN_OK_IF_SKIP(cctx);
1078 if ((isn = generate_instr(cctx, isn_type)) == NULL)
1079 return FAIL;
1080 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1081 isn->isn_arg.unlet.ul_forceit = forceit;
1082
1083 return OK;
1084}
1085
1086/*
1087 * Generate an ISN_LOCKCONST instruction.
1088 */
1089 int
1090generate_LOCKCONST(cctx_T *cctx)
1091{
1092 isn_T *isn;
1093
1094 RETURN_OK_IF_SKIP(cctx);
1095 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1096 return FAIL;
1097 return OK;
1098}
1099
1100/*
1101 * Generate an ISN_LOADS instruction.
1102 */
1103 int
1104generate_OLDSCRIPT(
1105 cctx_T *cctx,
1106 isntype_T isn_type,
1107 char_u *name,
1108 int sid,
1109 type_T *type)
1110{
1111 isn_T *isn;
1112
1113 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001114 if (isn_type == ISN_LOADS || isn_type == ISN_LOADEXPORT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001115 isn = generate_instr_type(cctx, isn_type, type);
1116 else
1117 isn = generate_instr_drop(cctx, isn_type, 1);
1118 if (isn == NULL)
1119 return FAIL;
1120 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1121 isn->isn_arg.loadstore.ls_sid = sid;
1122
1123 return OK;
1124}
1125
1126/*
1127 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1128 */
1129 int
1130generate_VIM9SCRIPT(
1131 cctx_T *cctx,
1132 isntype_T isn_type,
1133 int sid,
1134 int idx,
1135 type_T *type)
1136{
1137 isn_T *isn;
1138 scriptref_T *sref;
1139 scriptitem_T *si = SCRIPT_ITEM(sid);
1140
1141 RETURN_OK_IF_SKIP(cctx);
1142 if (isn_type == ISN_LOADSCRIPT)
Bram Moolenaar160afdb2022-02-06 17:17:02 +00001143 isn = generate_instr_type2(cctx, isn_type, type, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001144 else
1145 isn = generate_instr_drop(cctx, isn_type, 1);
1146 if (isn == NULL)
1147 return FAIL;
1148
1149 // This requires three arguments, which doesn't fit in an instruction, thus
1150 // we need to allocate a struct for this.
1151 sref = ALLOC_ONE(scriptref_T);
1152 if (sref == NULL)
1153 return FAIL;
1154 isn->isn_arg.script.scriptref = sref;
1155 sref->sref_sid = sid;
1156 sref->sref_idx = idx;
1157 sref->sref_seq = si->sn_script_seq;
1158 sref->sref_type = type;
1159 return OK;
1160}
1161
1162/*
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001163 * Generate an ISN_NEWLIST instruction for "count" items.
1164 * "use_null" is TRUE for null_list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001165 */
1166 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001167generate_NEWLIST(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001168{
1169 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001170 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001171 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001172 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001173
1174 RETURN_OK_IF_SKIP(cctx);
1175 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1176 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001177 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001178
Bram Moolenaar078a4612022-01-04 15:17:03 +00001179 // Get the member type and the declared member type from all the items on
1180 // the stack.
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001181 member_type = get_member_type_from_stack(count, 1, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001182 type = get_list_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001183 decl_type = get_list_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001184
1185 // drop the value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001186 cctx->ctx_type_stack.ga_len -= count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001187
1188 // add the list type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001189 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001190}
1191
1192/*
1193 * Generate an ISN_NEWDICT instruction.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001194 * "use_null" is TRUE for null_dict.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001195 */
1196 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001197generate_NEWDICT(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001198{
1199 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001200 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001201 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001202 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001203
1204 RETURN_OK_IF_SKIP(cctx);
1205 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1206 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001207 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001208
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001209 member_type = get_member_type_from_stack(count, 2, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001210 type = get_dict_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001211 decl_type = get_dict_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001212
1213 // drop the key and value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001214 cctx->ctx_type_stack.ga_len -= 2 * count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001215
1216 // add the dict type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001217 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001218}
1219
1220/*
1221 * Generate an ISN_FUNCREF instruction.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001222 * "isnp" is set to the instruction, so that fr_dfunc_idx can be set later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001223 */
1224 int
Bram Moolenaara915fa02022-03-23 11:29:15 +00001225generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc, isn_T **isnp)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001226{
1227 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001228 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001229
1230 RETURN_OK_IF_SKIP(cctx);
1231 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1232 return FAIL;
Bram Moolenaara915fa02022-03-23 11:29:15 +00001233 if (isnp != NULL)
1234 *isnp = isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001235 if (ufunc->uf_def_status == UF_NOT_COMPILED)
1236 isn->isn_arg.funcref.fr_func_name = vim_strsave(ufunc->uf_name);
1237 else
1238 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
1239 cctx->ctx_has_closure = 1;
1240
1241 // If the referenced function is a closure, it may use items further up in
Bram Moolenaar139575d2022-03-15 19:29:30 +00001242 // the nested context, including this one. But not a function defined at
1243 // the script level.
1244 if ((ufunc->uf_flags & FC_CLOSURE)
1245 && func_name_refcount(cctx->ctx_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001246 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1247
Bram Moolenaar078a4612022-01-04 15:17:03 +00001248 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1249 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001250}
1251
1252/*
1253 * Generate an ISN_NEWFUNC instruction.
1254 * "lambda_name" and "func_name" must be in allocated memory and will be
1255 * consumed.
1256 */
1257 int
1258generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1259{
1260 isn_T *isn;
1261
1262 if (cctx->ctx_skip == SKIP_YES)
1263 {
1264 vim_free(lambda_name);
1265 vim_free(func_name);
1266 return OK;
1267 }
1268 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1269 {
1270 vim_free(lambda_name);
1271 vim_free(func_name);
1272 return FAIL;
1273 }
1274 isn->isn_arg.newfunc.nf_lambda = lambda_name;
1275 isn->isn_arg.newfunc.nf_global = func_name;
1276
1277 return OK;
1278}
1279
1280/*
1281 * Generate an ISN_DEF instruction: list functions
1282 */
1283 int
1284generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1285{
1286 isn_T *isn;
1287
1288 RETURN_OK_IF_SKIP(cctx);
1289 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1290 return FAIL;
1291 if (len > 0)
1292 {
1293 isn->isn_arg.string = vim_strnsave(name, len);
1294 if (isn->isn_arg.string == NULL)
1295 return FAIL;
1296 }
1297 return OK;
1298}
1299
1300/*
1301 * Generate an ISN_JUMP instruction.
1302 */
1303 int
1304generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1305{
1306 isn_T *isn;
1307 garray_T *stack = &cctx->ctx_type_stack;
1308
1309 RETURN_OK_IF_SKIP(cctx);
1310 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1311 return FAIL;
1312 isn->isn_arg.jump.jump_when = when;
1313 isn->isn_arg.jump.jump_where = where;
1314
1315 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1316 --stack->ga_len;
1317
1318 return OK;
1319}
1320
1321/*
1322 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1323 */
1324 int
1325generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1326{
1327 isn_T *isn;
1328
1329 RETURN_OK_IF_SKIP(cctx);
1330 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1331 return FAIL;
1332 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1333 // jump_where is set later
1334 return OK;
1335}
1336
1337 int
1338generate_FOR(cctx_T *cctx, int loop_idx)
1339{
1340 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001341
1342 RETURN_OK_IF_SKIP(cctx);
1343 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1344 return FAIL;
1345 isn->isn_arg.forloop.for_idx = loop_idx;
1346
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001347 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001348 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001349}
1350/*
1351 * Generate an ISN_TRYCONT instruction.
1352 */
1353 int
1354generate_TRYCONT(cctx_T *cctx, int levels, int where)
1355{
1356 isn_T *isn;
1357
1358 RETURN_OK_IF_SKIP(cctx);
1359 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1360 return FAIL;
1361 isn->isn_arg.trycont.tct_levels = levels;
1362 isn->isn_arg.trycont.tct_where = where;
1363
1364 return OK;
1365}
1366
1367
1368/*
1369 * Generate an ISN_BCALL instruction.
1370 * "method_call" is TRUE for "value->method()"
1371 * Return FAIL if the number of arguments is wrong.
1372 */
1373 int
1374generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1375{
1376 isn_T *isn;
1377 garray_T *stack = &cctx->ctx_type_stack;
1378 int argoff;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001379 type2_T *typep;
1380 type2_T *argtypes = NULL;
1381 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1382 type2_T *maptype = NULL;
1383 type_T *type;
Bram Moolenaar81330182022-02-01 12:11:58 +00001384 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001385
1386 RETURN_OK_IF_SKIP(cctx);
1387 argoff = check_internal_func(func_idx, argcount);
1388 if (argoff < 0)
1389 return FAIL;
1390
1391 if (method_call && argoff > 1)
1392 {
1393 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1394 return FAIL;
1395 isn->isn_arg.shuffle.shfl_item = argcount;
1396 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1397 }
1398
1399 if (argcount > 0)
1400 {
1401 // Check the types of the arguments.
Bram Moolenaar078a4612022-01-04 15:17:03 +00001402 typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001403 if (method_call && argoff > 1)
1404 {
1405 int i;
1406
1407 for (i = 0; i < argcount; ++i)
1408 shuffled_argtypes[i] = (i < argoff - 1)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001409 ? typep[i + 1]
1410 : (i == argoff - 1) ? typep[0] : typep[i];
1411 argtypes = shuffled_argtypes;
1412 }
1413 else
1414 {
1415 int i;
1416
1417 for (i = 0; i < argcount; ++i)
1418 shuffled_argtypes[i] = typep[i];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001419 argtypes = shuffled_argtypes;
1420 }
1421 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1422 cctx) == FAIL)
1423 return FAIL;
1424 if (internal_func_is_map(func_idx))
Bram Moolenaar078a4612022-01-04 15:17:03 +00001425 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001426 }
1427
1428 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1429 return FAIL;
1430 isn->isn_arg.bfunc.cbf_idx = func_idx;
1431 isn->isn_arg.bfunc.cbf_argcount = argcount;
1432
1433 // Drop the argument types and push the return type.
1434 stack->ga_len -= argcount;
Bram Moolenaar81330182022-02-01 12:11:58 +00001435 type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type);
1436 if (push_type_stack2(cctx, type, decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001437 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001438
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001439 if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
1440 && maptype[0].type_decl->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001441 // Check that map() didn't change the item types.
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01001442 generate_TYPECHECK(cctx, maptype[0].type_decl, -1, FALSE, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001443
1444 return OK;
1445}
1446
1447/*
1448 * Generate an ISN_LISTAPPEND instruction. Works like add().
1449 * Argument count is already checked.
1450 */
1451 int
1452generate_LISTAPPEND(cctx_T *cctx)
1453{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001454 type_T *list_type;
1455 type_T *item_type;
1456 type_T *expected;
1457
1458 // Caller already checked that list_type is a list.
Bram Moolenaar81330182022-02-01 12:11:58 +00001459 // For checking the item type we use the declared type of the list and the
1460 // current type of the added item, adding a string to [1, 2] is OK.
1461 list_type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001462 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001463 expected = list_type->tt_member;
1464 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
1465 return FAIL;
1466
1467 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1468 return FAIL;
1469
Bram Moolenaar078a4612022-01-04 15:17:03 +00001470 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001471 return OK;
1472}
1473
1474/*
1475 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1476 * Argument count is already checked.
1477 */
1478 int
1479generate_BLOBAPPEND(cctx_T *cctx)
1480{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001481 type_T *item_type;
1482
1483 // Caller already checked that blob_type is a blob.
Bram Moolenaar078a4612022-01-04 15:17:03 +00001484 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001485 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
1486 return FAIL;
1487
1488 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1489 return FAIL;
1490
Bram Moolenaar078a4612022-01-04 15:17:03 +00001491 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001492 return OK;
1493}
1494
1495/*
1496 * Generate an ISN_DCALL or ISN_UCALL instruction.
1497 * Return FAIL if the number of arguments is wrong.
1498 */
1499 int
1500generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
1501{
1502 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001503 int regular_args = ufunc->uf_args.ga_len;
1504 int argcount = pushed_argcount;
1505
1506 RETURN_OK_IF_SKIP(cctx);
1507 if (argcount > regular_args && !has_varargs(ufunc))
1508 {
1509 semsg(_(e_too_many_arguments_for_function_str),
1510 printable_func_name(ufunc));
1511 return FAIL;
1512 }
1513 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1514 {
1515 semsg(_(e_not_enough_arguments_for_function_str),
1516 printable_func_name(ufunc));
1517 return FAIL;
1518 }
1519
1520 if (ufunc->uf_def_status != UF_NOT_COMPILED
1521 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1522 {
1523 int i;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001524 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001525
1526 for (i = 0; i < argcount; ++i)
1527 {
1528 type_T *expected;
1529 type_T *actual;
1530
Bram Moolenaar078a4612022-01-04 15:17:03 +00001531 actual = get_type_on_stack(cctx, argcount - i - 1);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001532 if (actual->tt_type == VAR_SPECIAL
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001533 && i >= regular_args - ufunc->uf_def_args.ga_len)
1534 {
1535 // assume v:none used for default argument value
1536 continue;
1537 }
1538 if (i < regular_args)
1539 {
1540 if (ufunc->uf_arg_types == NULL)
1541 continue;
1542 expected = ufunc->uf_arg_types[i];
1543 }
1544 else if (ufunc->uf_va_type == NULL
1545 || ufunc->uf_va_type == &t_list_any)
1546 // possibly a lambda or "...: any"
1547 expected = &t_any;
1548 else
1549 expected = ufunc->uf_va_type->tt_member;
1550 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
1551 TRUE, FALSE) == FAIL)
1552 {
1553 arg_type_mismatch(expected, actual, i + 1);
1554 return FAIL;
1555 }
1556 }
Bram Moolenaar139575d2022-03-15 19:29:30 +00001557 compile_type = get_compile_type(ufunc);
1558 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001559 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaar139575d2022-03-15 19:29:30 +00001560 compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001561 return FAIL;
1562 }
1563 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1564 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001565 emsg_funcname(e_call_to_function_that_failed_to_compile_str,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001566 ufunc->uf_name);
1567 return FAIL;
1568 }
1569
1570 if ((isn = generate_instr(cctx,
1571 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
1572 : ISN_UCALL)) == NULL)
1573 return FAIL;
1574 if (isn->isn_type == ISN_DCALL)
1575 {
1576 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1577 isn->isn_arg.dfunc.cdf_argcount = argcount;
1578 }
1579 else
1580 {
1581 // A user function may be deleted and redefined later, can't use the
1582 // ufunc pointer, need to look it up again at runtime.
1583 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1584 isn->isn_arg.ufunc.cuf_argcount = argcount;
1585 }
1586
Bram Moolenaar078a4612022-01-04 15:17:03 +00001587 // drop the argument types
1588 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001589
Bram Moolenaar078a4612022-01-04 15:17:03 +00001590 // add return type
1591 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001592}
1593
1594/*
1595 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1596 */
1597 int
1598generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1599{
1600 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001601
1602 RETURN_OK_IF_SKIP(cctx);
1603 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1604 return FAIL;
1605 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1606 isn->isn_arg.ufunc.cuf_argcount = argcount;
1607
Bram Moolenaar078a4612022-01-04 15:17:03 +00001608 // drop the argument types
1609 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001610
Bram Moolenaar078a4612022-01-04 15:17:03 +00001611 // add return value
1612 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001613}
1614
1615/*
1616 * Generate an ISN_PCALL instruction.
1617 * "type" is the type of the FuncRef.
1618 */
1619 int
1620generate_PCALL(
1621 cctx_T *cctx,
1622 int argcount,
1623 char_u *name,
1624 type_T *type,
1625 int at_top)
1626{
1627 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001628 type_T *ret_type;
1629
1630 RETURN_OK_IF_SKIP(cctx);
1631
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001632 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001633 ret_type = &t_any;
1634 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1635 {
1636 if (type->tt_argcount != -1)
1637 {
1638 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1639
1640 if (argcount < type->tt_min_argcount - varargs)
1641 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001642 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001643 return FAIL;
1644 }
1645 if (!varargs && argcount > type->tt_argcount)
1646 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001647 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001648 return FAIL;
1649 }
1650 if (type->tt_args != NULL)
1651 {
1652 int i;
1653
1654 for (i = 0; i < argcount; ++i)
1655 {
1656 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001657 type_T *actual = get_type_on_stack(cctx, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001658 type_T *expected;
1659
1660 if (varargs && i >= type->tt_argcount - 1)
1661 expected = type->tt_args[
1662 type->tt_argcount - 1]->tt_member;
1663 else if (i >= type->tt_min_argcount
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001664 && actual->tt_type == VAR_SPECIAL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001665 expected = &t_any;
1666 else
1667 expected = type->tt_args[i];
1668 if (need_type(actual, expected, offset, i + 1,
1669 cctx, TRUE, FALSE) == FAIL)
1670 {
1671 arg_type_mismatch(expected, actual, i + 1);
1672 return FAIL;
1673 }
1674 }
1675 }
1676 }
1677 ret_type = type->tt_member;
1678 if (ret_type == &t_unknown)
1679 // return type not known yet, use a runtime check
1680 ret_type = &t_any;
1681 }
1682 else
1683 {
1684 semsg(_(e_not_callable_type_str), name);
1685 return FAIL;
1686 }
1687
1688 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1689 return FAIL;
1690 isn->isn_arg.pfunc.cpf_top = at_top;
1691 isn->isn_arg.pfunc.cpf_argcount = argcount;
1692
Bram Moolenaar078a4612022-01-04 15:17:03 +00001693 // drop the arguments and the funcref/partial
1694 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001695
Bram Moolenaar078a4612022-01-04 15:17:03 +00001696 // push the return value
1697 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001698
1699 // If partial is above the arguments it must be cleared and replaced with
1700 // the return value.
1701 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1702 return FAIL;
1703
1704 return OK;
1705}
1706
1707/*
1708 * Generate an ISN_STRINGMEMBER instruction.
1709 */
1710 int
1711generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
1712{
1713 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001714 type_T *type;
1715
1716 RETURN_OK_IF_SKIP(cctx);
1717 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
1718 return FAIL;
1719 isn->isn_arg.string = vim_strnsave(name, len);
1720
1721 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00001722 type = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001723 if (type->tt_type != VAR_DICT && type != &t_any && type != &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001724 {
1725 char *tofree;
1726
1727 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
1728 name, type_name(type, &tofree));
1729 vim_free(tofree);
1730 return FAIL;
1731 }
1732 // change dict type to dict member type
1733 if (type->tt_type == VAR_DICT)
1734 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00001735 type_T *ntype = type->tt_member == &t_unknown
1736 ? &t_any : type->tt_member;
1737 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001738 }
1739
1740 return OK;
1741}
1742
1743/*
1744 * Generate an ISN_ECHO instruction.
1745 */
1746 int
1747generate_ECHO(cctx_T *cctx, int with_white, int count)
1748{
1749 isn_T *isn;
1750
1751 RETURN_OK_IF_SKIP(cctx);
1752 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1753 return FAIL;
1754 isn->isn_arg.echo.echo_with_white = with_white;
1755 isn->isn_arg.echo.echo_count = count;
1756
1757 return OK;
1758}
1759
1760/*
1761 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
1762 */
1763 int
1764generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
1765{
1766 isn_T *isn;
1767
1768 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
1769 return FAIL;
1770 isn->isn_arg.number = count;
1771
1772 return OK;
1773}
1774
1775/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001776 * Generate an ISN_SOURCE instruction.
1777 */
1778 int
1779generate_SOURCE(cctx_T *cctx, int sid)
1780{
1781 isn_T *isn;
1782
1783 if ((isn = generate_instr(cctx, ISN_SOURCE)) == NULL)
1784 return FAIL;
1785 isn->isn_arg.number = sid;
1786
1787 return OK;
1788}
1789
1790/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001791 * Generate an ISN_PUT instruction.
1792 */
1793 int
1794generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1795{
1796 isn_T *isn;
1797
1798 RETURN_OK_IF_SKIP(cctx);
1799 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1800 return FAIL;
1801 isn->isn_arg.put.put_regname = regname;
1802 isn->isn_arg.put.put_lnum = lnum;
1803 return OK;
1804}
1805
1806/*
1807 * Generate an EXEC instruction that takes a string argument.
1808 * A copy is made of "line".
1809 */
1810 int
1811generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
1812{
1813 isn_T *isn;
1814
1815 RETURN_OK_IF_SKIP(cctx);
1816 if ((isn = generate_instr(cctx, isntype)) == NULL)
1817 return FAIL;
1818 isn->isn_arg.string = vim_strsave(line);
1819 return OK;
1820}
1821
1822/*
1823 * Generate an EXEC instruction that takes a string argument.
1824 * "str" must be allocated, it is consumed.
1825 */
1826 int
1827generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
1828{
1829 isn_T *isn;
1830
1831 if (cctx->ctx_skip == SKIP_YES)
1832 {
1833 vim_free(str);
1834 return OK;
1835 }
1836 if ((isn = generate_instr(cctx, isntype)) == NULL)
1837 {
1838 vim_free(str);
1839 return FAIL;
1840 }
1841 isn->isn_arg.string = str;
1842 return OK;
1843}
1844
1845 int
1846generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
1847{
1848 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001849
1850 RETURN_OK_IF_SKIP(cctx);
1851 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
1852 return FAIL;
1853 isn->isn_arg.string = vim_strsave(line);
1854
Bram Moolenaar078a4612022-01-04 15:17:03 +00001855 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001856}
1857
1858 int
1859generate_EXECCONCAT(cctx_T *cctx, int count)
1860{
1861 isn_T *isn;
1862
1863 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1864 return FAIL;
1865 isn->isn_arg.number = count;
1866 return OK;
1867}
1868
1869/*
1870 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
1871 */
1872 int
1873generate_RANGE(cctx_T *cctx, char_u *range)
1874{
1875 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001876
1877 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
1878 return FAIL;
1879 isn->isn_arg.string = range;
1880
Bram Moolenaar078a4612022-01-04 15:17:03 +00001881 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001882}
1883
1884 int
1885generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
1886{
1887 isn_T *isn;
1888
1889 RETURN_OK_IF_SKIP(cctx);
1890 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
1891 return FAIL;
1892 isn->isn_arg.unpack.unp_count = var_count;
1893 isn->isn_arg.unpack.unp_semicolon = semicolon;
1894 return OK;
1895}
1896
1897/*
1898 * Generate an instruction for any command modifiers.
1899 */
1900 int
1901generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
1902{
1903 isn_T *isn;
1904
1905 if (has_cmdmod(cmod, FALSE))
1906 {
1907 cctx->ctx_has_cmdmod = TRUE;
1908
1909 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
1910 return FAIL;
1911 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
1912 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
1913 return FAIL;
1914 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
1915 // filter program now belongs to the instruction
1916 cmod->cmod_filter_regmatch.regprog = NULL;
1917 }
1918
1919 return OK;
1920}
1921
1922 int
1923generate_undo_cmdmods(cctx_T *cctx)
1924{
1925 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
1926 return FAIL;
1927 cctx->ctx_has_cmdmod = FALSE;
1928 return OK;
1929}
1930
1931/*
1932 * Generate a STORE instruction for "dest", not being "dest_local".
1933 * Return FAIL when out of memory.
1934 */
1935 int
1936generate_store_var(
1937 cctx_T *cctx,
1938 assign_dest_T dest,
1939 int opt_flags,
1940 int vimvaridx,
1941 int scriptvar_idx,
1942 int scriptvar_sid,
1943 type_T *type,
1944 char_u *name)
1945{
1946 switch (dest)
1947 {
1948 case dest_option:
1949 return generate_STOREOPT(cctx, ISN_STOREOPT,
1950 skip_option_env_lead(name), opt_flags);
1951 case dest_func_option:
1952 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
1953 skip_option_env_lead(name), opt_flags);
1954 case dest_global:
1955 // include g: with the name, easier to execute that way
1956 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
1957 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
1958 case dest_buffer:
1959 // include b: with the name, easier to execute that way
1960 return generate_STORE(cctx, ISN_STOREB, 0, name);
1961 case dest_window:
1962 // include w: with the name, easier to execute that way
1963 return generate_STORE(cctx, ISN_STOREW, 0, name);
1964 case dest_tab:
1965 // include t: with the name, easier to execute that way
1966 return generate_STORE(cctx, ISN_STORET, 0, name);
1967 case dest_env:
1968 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
1969 case dest_reg:
1970 return generate_STORE(cctx, ISN_STOREREG,
1971 name[1] == '@' ? '"' : name[1], NULL);
1972 case dest_vimvar:
1973 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
1974 case dest_script:
1975 if (scriptvar_idx < 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001976 {
1977 isntype_T isn_type = ISN_STORES;
1978
1979 if (SCRIPT_ID_VALID(scriptvar_sid)
Bram Moolenaarccbfd482022-03-31 16:18:23 +01001980 && SCRIPT_ITEM(scriptvar_sid)->sn_import_autoload
1981 && SCRIPT_ITEM(scriptvar_sid)->sn_autoload_prefix
1982 == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001983 {
1984 // "import autoload './dir/script.vim'" - load script first
1985 if (generate_SOURCE(cctx, scriptvar_sid) == FAIL)
1986 return FAIL;
1987 isn_type = ISN_STOREEXPORT;
1988 }
1989
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001990 // "s:" may be included in the name.
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001991 return generate_OLDSCRIPT(cctx, isn_type, name,
1992 scriptvar_sid, type);
1993 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001994 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
1995 scriptvar_sid, scriptvar_idx, type);
1996 case dest_local:
1997 case dest_expr:
1998 // cannot happen
1999 break;
2000 }
2001 return FAIL;
2002}
2003
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002004/*
2005 * Return TRUE when inside a "for" or "while" loop.
2006 */
2007 int
2008inside_loop_scope(cctx_T *cctx)
2009{
2010 scope_T *scope = cctx->ctx_scope;
2011
2012 for (;;)
2013 {
2014 if (scope == NULL)
2015 break;
2016 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
2017 return TRUE;
2018 scope = scope->se_outer;
2019 }
2020 return FALSE;
2021}
2022
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002023 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002024generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002025{
2026 if (lhs->lhs_dest != dest_local)
2027 return generate_store_var(cctx, lhs->lhs_dest,
2028 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
2029 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
2030 lhs->lhs_type, lhs->lhs_name);
2031
2032 if (lhs->lhs_lvar != NULL)
2033 {
2034 garray_T *instr = &cctx->ctx_instr;
2035 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2036
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002037 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
2038 // ISN_STORENR.
2039 // And "var = 0" does not need any instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002040 if (lhs->lhs_lvar->lv_from_outer == 0
2041 && instr->ga_len == instr_count + 1
2042 && isn->isn_type == ISN_PUSHNR)
2043 {
2044 varnumber_T val = isn->isn_arg.number;
2045 garray_T *stack = &cctx->ctx_type_stack;
2046
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002047 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002048 {
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002049 // zero is the default value, no need to do anything
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002050 --instr->ga_len;
2051 }
2052 else
2053 {
2054 isn->isn_type = ISN_STORENR;
2055 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
2056 isn->isn_arg.storenr.stnr_val = val;
2057 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002058 if (stack->ga_len > 0)
2059 --stack->ga_len;
2060 }
2061 else if (lhs->lhs_lvar->lv_from_outer > 0)
2062 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
2063 lhs->lhs_lvar->lv_from_outer);
2064 else
2065 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
2066 }
2067 return OK;
2068}
2069
2070#if defined(FEAT_PROFILE) || defined(PROTO)
2071 void
2072may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2073{
2074 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
2075 generate_instr(cctx, ISN_PROF_END);
2076}
2077#endif
2078
2079
2080/*
2081 * Delete an instruction, free what it contains.
2082 */
2083 void
2084delete_instr(isn_T *isn)
2085{
2086 switch (isn->isn_type)
2087 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002088 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002089 case ISN_DEF:
2090 case ISN_EXEC:
2091 case ISN_EXECRANGE:
2092 case ISN_EXEC_SPLIT:
2093 case ISN_LEGACY_EVAL:
2094 case ISN_LOADAUTO:
2095 case ISN_LOADB:
2096 case ISN_LOADENV:
2097 case ISN_LOADG:
2098 case ISN_LOADOPT:
2099 case ISN_LOADT:
2100 case ISN_LOADW:
2101 case ISN_LOCKUNLOCK:
2102 case ISN_PUSHEXC:
2103 case ISN_PUSHFUNC:
2104 case ISN_PUSHS:
2105 case ISN_RANGE:
2106 case ISN_STOREAUTO:
2107 case ISN_STOREB:
2108 case ISN_STOREENV:
2109 case ISN_STOREG:
2110 case ISN_STORET:
2111 case ISN_STOREW:
2112 case ISN_STRINGMEMBER:
2113 vim_free(isn->isn_arg.string);
2114 break;
2115
2116 case ISN_SUBSTITUTE:
2117 {
2118 int idx;
2119 isn_T *list = isn->isn_arg.subs.subs_instr;
2120
2121 vim_free(isn->isn_arg.subs.subs_cmd);
2122 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2123 delete_instr(list + idx);
2124 vim_free(list);
2125 }
2126 break;
2127
2128 case ISN_INSTR:
2129 {
2130 int idx;
2131 isn_T *list = isn->isn_arg.instr;
2132
2133 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2134 delete_instr(list + idx);
2135 vim_free(list);
2136 }
2137 break;
2138
2139 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002140 case ISN_LOADEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002141 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002142 case ISN_STOREEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002143 vim_free(isn->isn_arg.loadstore.ls_name);
2144 break;
2145
2146 case ISN_UNLET:
2147 case ISN_UNLETENV:
2148 vim_free(isn->isn_arg.unlet.ul_name);
2149 break;
2150
2151 case ISN_STOREOPT:
2152 case ISN_STOREFUNCOPT:
2153 vim_free(isn->isn_arg.storeopt.so_name);
2154 break;
2155
2156 case ISN_PUSHBLOB: // push blob isn_arg.blob
2157 blob_unref(isn->isn_arg.blob);
2158 break;
2159
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002160 case ISN_UCALL:
2161 vim_free(isn->isn_arg.ufunc.cuf_name);
2162 break;
2163
2164 case ISN_FUNCREF:
2165 {
2166 if (isn->isn_arg.funcref.fr_func_name == NULL)
2167 {
2168 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002169 + isn->isn_arg.funcref.fr_dfunc_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002170 ufunc_T *ufunc = dfunc->df_ufunc;
2171
2172 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2173 func_ptr_unref(ufunc);
2174 }
2175 else
2176 {
2177 char_u *name = isn->isn_arg.funcref.fr_func_name;
2178
2179 if (name != NULL)
2180 func_unref(name);
2181 vim_free(isn->isn_arg.funcref.fr_func_name);
2182 }
2183 }
2184 break;
2185
2186 case ISN_DCALL:
2187 {
2188 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002189 + isn->isn_arg.dfunc.cdf_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002190
2191 if (dfunc->df_ufunc != NULL
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002192 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002193 func_ptr_unref(dfunc->df_ufunc);
2194 }
2195 break;
2196
2197 case ISN_NEWFUNC:
2198 {
2199 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002200 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002201
2202 if (ufunc != NULL)
2203 {
2204 unlink_def_function(ufunc);
2205 func_ptr_unref(ufunc);
2206 }
2207
2208 vim_free(lambda);
2209 vim_free(isn->isn_arg.newfunc.nf_global);
2210 }
2211 break;
2212
2213 case ISN_CHECKTYPE:
2214 case ISN_SETTYPE:
2215 free_type(isn->isn_arg.type.ct_type);
2216 break;
2217
2218 case ISN_CMDMOD:
2219 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002220 ->cmod_filter_regmatch.regprog);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002221 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2222 break;
2223
2224 case ISN_LOADSCRIPT:
2225 case ISN_STORESCRIPT:
2226 vim_free(isn->isn_arg.script.scriptref);
2227 break;
2228
2229 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002230 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002231 break;
2232
2233 case ISN_CEXPR_CORE:
2234 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2235 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2236 break;
2237
2238 case ISN_2BOOL:
2239 case ISN_2STRING:
2240 case ISN_2STRING_ANY:
2241 case ISN_ADDBLOB:
2242 case ISN_ADDLIST:
2243 case ISN_ANYINDEX:
2244 case ISN_ANYSLICE:
2245 case ISN_BCALL:
2246 case ISN_BLOBAPPEND:
2247 case ISN_BLOBINDEX:
2248 case ISN_BLOBSLICE:
2249 case ISN_CATCH:
2250 case ISN_CEXPR_AUCMD:
2251 case ISN_CHECKLEN:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002252 case ISN_CLEARDICT:
2253 case ISN_CMDMOD_REV:
2254 case ISN_COMPAREANY:
2255 case ISN_COMPAREBLOB:
2256 case ISN_COMPAREBOOL:
2257 case ISN_COMPAREDICT:
2258 case ISN_COMPAREFLOAT:
2259 case ISN_COMPAREFUNC:
2260 case ISN_COMPARELIST:
2261 case ISN_COMPARENR:
Bram Moolenaar7a222242022-03-01 19:23:24 +00002262 case ISN_COMPARENULL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002263 case ISN_COMPARESPECIAL:
2264 case ISN_COMPARESTRING:
2265 case ISN_CONCAT:
2266 case ISN_COND2BOOL:
2267 case ISN_DEBUG:
2268 case ISN_DROP:
2269 case ISN_ECHO:
2270 case ISN_ECHOCONSOLE:
2271 case ISN_ECHOERR:
2272 case ISN_ECHOMSG:
2273 case ISN_ENDTRY:
2274 case ISN_EXECCONCAT:
2275 case ISN_EXECUTE:
2276 case ISN_FINALLY:
2277 case ISN_FINISH:
2278 case ISN_FOR:
2279 case ISN_GETITEM:
2280 case ISN_JUMP:
2281 case ISN_JUMP_IF_ARG_SET:
2282 case ISN_LISTAPPEND:
2283 case ISN_LISTINDEX:
2284 case ISN_LISTSLICE:
2285 case ISN_LOAD:
2286 case ISN_LOADBDICT:
2287 case ISN_LOADGDICT:
2288 case ISN_LOADOUTER:
2289 case ISN_LOADREG:
2290 case ISN_LOADTDICT:
2291 case ISN_LOADV:
2292 case ISN_LOADWDICT:
2293 case ISN_LOCKCONST:
2294 case ISN_MEMBER:
2295 case ISN_NEGATENR:
2296 case ISN_NEWDICT:
2297 case ISN_NEWLIST:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002298 case ISN_NEWPARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002299 case ISN_OPANY:
2300 case ISN_OPFLOAT:
2301 case ISN_OPNR:
2302 case ISN_PCALL:
2303 case ISN_PCALL_END:
2304 case ISN_PROF_END:
2305 case ISN_PROF_START:
2306 case ISN_PUSHBOOL:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002307 case ISN_PUSHCHANNEL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002308 case ISN_PUSHF:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002309 case ISN_PUSHJOB:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002310 case ISN_PUSHNR:
2311 case ISN_PUSHSPEC:
2312 case ISN_PUT:
2313 case ISN_REDIREND:
2314 case ISN_REDIRSTART:
2315 case ISN_RETURN:
2316 case ISN_RETURN_VOID:
2317 case ISN_SHUFFLE:
2318 case ISN_SLICE:
2319 case ISN_STORE:
2320 case ISN_STOREINDEX:
2321 case ISN_STORENR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002322 case ISN_SOURCE:
2323 case ISN_STOREOUTER:
2324 case ISN_STORERANGE:
2325 case ISN_STOREREG:
2326 case ISN_STOREV:
2327 case ISN_STRINDEX:
2328 case ISN_STRSLICE:
2329 case ISN_THROW:
2330 case ISN_TRYCONT:
2331 case ISN_UNLETINDEX:
2332 case ISN_UNLETRANGE:
2333 case ISN_UNPACK:
2334 case ISN_USEDICT:
2335 // nothing allocated
2336 break;
2337}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002338}
2339
2340 void
2341clear_instr_ga(garray_T *gap)
2342{
2343 int idx;
2344
2345 for (idx = 0; idx < gap->ga_len; ++idx)
2346 delete_instr(((isn_T *)gap->ga_data) + idx);
2347 ga_clear(gap);
2348}
2349
2350
2351#endif // defined(FEAT_EVAL)