blob: ab52d4c4c35c0456ad71d403281d76adf9285770 [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/*
475 * Generate an ISN_2BOOL instruction.
476 * "offset" is the offset in the type stack.
477 */
478 int
479generate_2BOOL(cctx_T *cctx, int invert, int offset)
480{
481 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000482
483 RETURN_OK_IF_SKIP(cctx);
484 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
485 return FAIL;
486 isn->isn_arg.tobool.invert = invert;
487 isn->isn_arg.tobool.offset = offset;
488
489 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000490 set_type_on_stack(cctx, &t_bool, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000491
492 return OK;
493}
494
495/*
496 * Generate an ISN_COND2BOOL instruction.
497 */
498 int
499generate_COND2BOOL(cctx_T *cctx)
500{
501 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000502
503 RETURN_OK_IF_SKIP(cctx);
504 if ((isn = generate_instr(cctx, ISN_COND2BOOL)) == NULL)
505 return FAIL;
506
507 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000508 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000509
510 return OK;
511}
512
513 int
514generate_TYPECHECK(
515 cctx_T *cctx,
516 type_T *expected,
517 int offset,
518 int argidx)
519{
520 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000521
522 RETURN_OK_IF_SKIP(cctx);
523 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
524 return FAIL;
525 isn->isn_arg.type.ct_type = alloc_type(expected);
526 isn->isn_arg.type.ct_off = (int8_T)offset;
527 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
528
529 // type becomes expected
Bram Moolenaar078a4612022-01-04 15:17:03 +0000530 set_type_on_stack(cctx, expected, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000531
532 return OK;
533}
534
535 int
536generate_SETTYPE(
537 cctx_T *cctx,
538 type_T *expected)
539{
540 isn_T *isn;
541
542 RETURN_OK_IF_SKIP(cctx);
543 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
544 return FAIL;
545 isn->isn_arg.type.ct_type = alloc_type(expected);
546 return OK;
547}
548
549/*
550 * Generate a PUSH instruction for "tv".
551 * "tv" will be consumed or cleared.
552 * Nothing happens if "tv" is NULL or of type VAR_UNKNOWN;
553 */
554 int
555generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
556{
557 if (tv != NULL)
558 {
559 switch (tv->v_type)
560 {
561 case VAR_UNKNOWN:
562 break;
563 case VAR_BOOL:
564 generate_PUSHBOOL(cctx, tv->vval.v_number);
565 break;
566 case VAR_SPECIAL:
567 generate_PUSHSPEC(cctx, tv->vval.v_number);
568 break;
569 case VAR_NUMBER:
570 generate_PUSHNR(cctx, tv->vval.v_number);
571 break;
572#ifdef FEAT_FLOAT
573 case VAR_FLOAT:
574 generate_PUSHF(cctx, tv->vval.v_float);
575 break;
576#endif
577 case VAR_BLOB:
578 generate_PUSHBLOB(cctx, tv->vval.v_blob);
579 tv->vval.v_blob = NULL;
580 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000581 case VAR_LIST:
582 if (tv->vval.v_list != NULL)
583 iemsg("non-empty list constant not supported");
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100584 generate_NEWLIST(cctx, 0, TRUE);
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000585 break;
586 case VAR_DICT:
587 if (tv->vval.v_dict != NULL)
588 iemsg("non-empty dict constant not supported");
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100589 generate_NEWDICT(cctx, 0, TRUE);
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000590 break;
591#ifdef FEAT_JOB_CHANNEL
592 case VAR_JOB:
593 if (tv->vval.v_job != NULL)
594 iemsg("non-null job constant not supported");
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000595 generate_PUSHJOB(cctx);
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000596 break;
597 case VAR_CHANNEL:
598 if (tv->vval.v_channel != NULL)
599 iemsg("non-null channel constant not supported");
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000600 generate_PUSHCHANNEL(cctx);
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000601 break;
602#endif
603 case VAR_FUNC:
604 if (tv->vval.v_string != NULL)
605 iemsg("non-null function constant not supported");
606 generate_PUSHFUNC(cctx, NULL, &t_func_unknown);
607 break;
608 case VAR_PARTIAL:
609 if (tv->vval.v_partial != NULL)
610 iemsg("non-null partial constant not supported");
611 if (generate_instr_type(cctx, ISN_NEWPARTIAL, &t_func_unknown)
612 == NULL)
613 return FAIL;
614 break;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000615 case VAR_STRING:
616 generate_PUSHS(cctx, &tv->vval.v_string);
617 tv->vval.v_string = NULL;
618 break;
619 default:
620 iemsg("constant type not supported");
621 clear_tv(tv);
622 return FAIL;
623 }
624 tv->v_type = VAR_UNKNOWN;
625 }
626 return OK;
627}
628
629/*
630 * Generate an ISN_PUSHNR instruction.
631 */
632 int
633generate_PUSHNR(cctx_T *cctx, varnumber_T number)
634{
635 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000636
637 RETURN_OK_IF_SKIP(cctx);
638 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
639 return FAIL;
640 isn->isn_arg.number = number;
641
642 if (number == 0 || number == 1)
643 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000644 set_type_on_stack(cctx, &t_number_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000645 return OK;
646}
647
648/*
649 * Generate an ISN_PUSHBOOL instruction.
650 */
651 int
652generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
653{
654 isn_T *isn;
655
656 RETURN_OK_IF_SKIP(cctx);
657 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
658 return FAIL;
659 isn->isn_arg.number = number;
660
661 return OK;
662}
663
664/*
665 * Generate an ISN_PUSHSPEC instruction.
666 */
667 int
668generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
669{
670 isn_T *isn;
671
672 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000673 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC,
674 number == VVAL_NULL ? &t_null : &t_none)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000675 return FAIL;
676 isn->isn_arg.number = number;
677
678 return OK;
679}
680
681#if defined(FEAT_FLOAT) || defined(PROTO)
682/*
683 * Generate an ISN_PUSHF instruction.
684 */
685 int
686generate_PUSHF(cctx_T *cctx, float_T fnumber)
687{
688 isn_T *isn;
689
690 RETURN_OK_IF_SKIP(cctx);
691 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
692 return FAIL;
693 isn->isn_arg.fnumber = fnumber;
694
695 return OK;
696}
697#endif
698
699/*
700 * Generate an ISN_PUSHS instruction.
701 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
702 */
703 int
704generate_PUSHS(cctx_T *cctx, char_u **str)
705{
706 isn_T *isn;
707
708 if (cctx->ctx_skip == SKIP_YES)
709 {
710 if (str != NULL)
711 VIM_CLEAR(*str);
712 return OK;
713 }
714 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
715 {
716 if (str != NULL)
717 VIM_CLEAR(*str);
718 return FAIL;
719 }
720 isn->isn_arg.string = str == NULL ? NULL : *str;
721
722 return OK;
723}
724
725/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000726 * Generate an ISN_PUSHCHANNEL instruction. Channel is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000727 */
728 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000729generate_PUSHCHANNEL(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000730{
731 isn_T *isn;
732
733 RETURN_OK_IF_SKIP(cctx);
734 if ((isn = generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel)) == NULL)
735 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000736 return OK;
737}
738
739/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000740 * Generate an ISN_PUSHJOB instruction. Job is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000741 */
742 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000743generate_PUSHJOB(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000744{
745 isn_T *isn;
746
747 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000748 if ((isn = generate_instr_type(cctx, ISN_PUSHJOB, &t_job)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000749 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000750 return OK;
751}
752
753/*
754 * Generate an ISN_PUSHBLOB instruction.
755 * Consumes "blob".
756 */
757 int
758generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
759{
760 isn_T *isn;
761
762 RETURN_OK_IF_SKIP(cctx);
763 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
764 return FAIL;
765 isn->isn_arg.blob = blob;
766
767 return OK;
768}
769
770/*
771 * Generate an ISN_PUSHFUNC instruction with name "name".
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000772 */
773 int
774generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type)
775{
776 isn_T *isn;
777 char_u *funcname;
778
779 RETURN_OK_IF_SKIP(cctx);
780 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
781 return FAIL;
782 if (name == NULL)
783 funcname = NULL;
Bram Moolenaard041f422022-01-12 19:54:00 +0000784 else if (*name == K_SPECIAL // script-local
785 || vim_strchr(name, AUTOLOAD_CHAR) != NULL) // autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000786 funcname = vim_strsave(name);
787 else
788 {
789 funcname = alloc(STRLEN(name) + 3);
790 if (funcname != NULL)
791 {
792 STRCPY(funcname, "g:");
793 STRCPY(funcname + 2, name);
794 }
795 }
796
797 isn->isn_arg.string = funcname;
798 return OK;
799}
800
801/*
Bram Moolenaar06b77222022-01-25 15:51:56 +0000802 * Generate an ISN_AUTOLOAD instruction.
803 */
804 int
805generate_AUTOLOAD(cctx_T *cctx, char_u *name, type_T *type)
806{
807 isn_T *isn;
808
809 RETURN_OK_IF_SKIP(cctx);
810 if ((isn = generate_instr_type(cctx, ISN_AUTOLOAD, type)) == NULL)
811 return FAIL;
812 isn->isn_arg.string = vim_strsave(name);
813 if (isn->isn_arg.string == NULL)
814 return FAIL;
815 return OK;
816}
817
818/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000819 * Generate an ISN_GETITEM instruction with "index".
820 * "with_op" is TRUE for "+=" and other operators, the stack has the current
821 * value below the list with values.
822 */
823 int
824generate_GETITEM(cctx_T *cctx, int index, int with_op)
825{
826 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000827 type_T *type = get_type_on_stack(cctx, with_op ? 1 : 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000828 type_T *item_type = &t_any;
829
830 RETURN_OK_IF_SKIP(cctx);
831
832 if (type->tt_type != VAR_LIST)
833 {
834 // cannot happen, caller has checked the type
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000835 emsg(_(e_list_required));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000836 return FAIL;
837 }
838 item_type = type->tt_member;
839 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
840 return FAIL;
841 isn->isn_arg.getitem.gi_index = index;
842 isn->isn_arg.getitem.gi_with_op = with_op;
843
844 // add the item type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +0000845 return push_type_stack(cctx, item_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000846}
847
848/*
849 * Generate an ISN_SLICE instruction with "count".
850 */
851 int
852generate_SLICE(cctx_T *cctx, int count)
853{
854 isn_T *isn;
855
856 RETURN_OK_IF_SKIP(cctx);
857 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
858 return FAIL;
859 isn->isn_arg.number = count;
860 return OK;
861}
862
863/*
864 * Generate an ISN_CHECKLEN instruction with "min_len".
865 */
866 int
867generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
868{
869 isn_T *isn;
870
871 RETURN_OK_IF_SKIP(cctx);
872
873 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
874 return FAIL;
875 isn->isn_arg.checklen.cl_min_len = min_len;
876 isn->isn_arg.checklen.cl_more_OK = more_OK;
877
878 return OK;
879}
880
881/*
882 * Generate an ISN_STORE instruction.
883 */
884 int
885generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
886{
887 isn_T *isn;
888
889 RETURN_OK_IF_SKIP(cctx);
890 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
891 return FAIL;
892 if (name != NULL)
893 isn->isn_arg.string = vim_strsave(name);
894 else
895 isn->isn_arg.number = idx;
896
897 return OK;
898}
899
900/*
901 * Generate an ISN_STOREOUTER instruction.
902 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000903 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000904generate_STOREOUTER(cctx_T *cctx, int idx, int level)
905{
906 isn_T *isn;
907
908 RETURN_OK_IF_SKIP(cctx);
909 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
910 return FAIL;
911 isn->isn_arg.outer.outer_idx = idx;
912 isn->isn_arg.outer.outer_depth = level;
913
914 return OK;
915}
916
917/*
918 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
919 */
920 int
921generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
922{
923 isn_T *isn;
924
925 RETURN_OK_IF_SKIP(cctx);
926 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
927 return FAIL;
928 isn->isn_arg.storenr.stnr_idx = idx;
929 isn->isn_arg.storenr.stnr_val = value;
930
931 return OK;
932}
933
934/*
935 * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction
936 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000937 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000938generate_STOREOPT(
939 cctx_T *cctx,
940 isntype_T isn_type,
941 char_u *name,
942 int opt_flags)
943{
944 isn_T *isn;
945
946 RETURN_OK_IF_SKIP(cctx);
947 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
948 return FAIL;
949 isn->isn_arg.storeopt.so_name = vim_strsave(name);
950 isn->isn_arg.storeopt.so_flags = opt_flags;
951
952 return OK;
953}
954
955/*
956 * Generate an ISN_LOAD or similar instruction.
957 */
958 int
959generate_LOAD(
960 cctx_T *cctx,
961 isntype_T isn_type,
962 int idx,
963 char_u *name,
964 type_T *type)
965{
966 isn_T *isn;
967
968 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +0000969 if ((isn = generate_instr_type2(cctx, isn_type, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000970 return FAIL;
971 if (name != NULL)
972 isn->isn_arg.string = vim_strsave(name);
973 else
974 isn->isn_arg.number = idx;
975
976 return OK;
977}
978
979/*
980 * Generate an ISN_LOADOUTER instruction
981 */
982 int
983generate_LOADOUTER(
984 cctx_T *cctx,
985 int idx,
986 int nesting,
987 type_T *type)
988{
989 isn_T *isn;
990
991 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +0000992 if ((isn = generate_instr_type2(cctx, ISN_LOADOUTER, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000993 return FAIL;
994 isn->isn_arg.outer.outer_idx = idx;
995 isn->isn_arg.outer.outer_depth = nesting;
996
997 return OK;
998}
999
1000/*
1001 * Generate an ISN_LOADV instruction for v:var.
1002 */
1003 int
1004generate_LOADV(
1005 cctx_T *cctx,
1006 char_u *name,
1007 int error)
1008{
1009 int di_flags;
1010 int vidx = find_vim_var(name, &di_flags);
1011 type_T *type;
1012
1013 RETURN_OK_IF_SKIP(cctx);
1014 if (vidx < 0)
1015 {
1016 if (error)
1017 semsg(_(e_variable_not_found_str), name);
1018 return FAIL;
1019 }
Bram Moolenaard787e402021-12-24 21:36:12 +00001020 type = get_vim_var_type(vidx, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001021 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
1022}
1023
1024/*
1025 * Generate an ISN_UNLET instruction.
1026 */
1027 int
1028generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
1029{
1030 isn_T *isn;
1031
1032 RETURN_OK_IF_SKIP(cctx);
1033 if ((isn = generate_instr(cctx, isn_type)) == NULL)
1034 return FAIL;
1035 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1036 isn->isn_arg.unlet.ul_forceit = forceit;
1037
1038 return OK;
1039}
1040
1041/*
1042 * Generate an ISN_LOCKCONST instruction.
1043 */
1044 int
1045generate_LOCKCONST(cctx_T *cctx)
1046{
1047 isn_T *isn;
1048
1049 RETURN_OK_IF_SKIP(cctx);
1050 if ((isn = generate_instr(cctx, ISN_LOCKCONST)) == NULL)
1051 return FAIL;
1052 return OK;
1053}
1054
1055/*
1056 * Generate an ISN_LOADS instruction.
1057 */
1058 int
1059generate_OLDSCRIPT(
1060 cctx_T *cctx,
1061 isntype_T isn_type,
1062 char_u *name,
1063 int sid,
1064 type_T *type)
1065{
1066 isn_T *isn;
1067
1068 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001069 if (isn_type == ISN_LOADS || isn_type == ISN_LOADEXPORT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001070 isn = generate_instr_type(cctx, isn_type, type);
1071 else
1072 isn = generate_instr_drop(cctx, isn_type, 1);
1073 if (isn == NULL)
1074 return FAIL;
1075 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1076 isn->isn_arg.loadstore.ls_sid = sid;
1077
1078 return OK;
1079}
1080
1081/*
1082 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1083 */
1084 int
1085generate_VIM9SCRIPT(
1086 cctx_T *cctx,
1087 isntype_T isn_type,
1088 int sid,
1089 int idx,
1090 type_T *type)
1091{
1092 isn_T *isn;
1093 scriptref_T *sref;
1094 scriptitem_T *si = SCRIPT_ITEM(sid);
1095
1096 RETURN_OK_IF_SKIP(cctx);
1097 if (isn_type == ISN_LOADSCRIPT)
Bram Moolenaar160afdb2022-02-06 17:17:02 +00001098 isn = generate_instr_type2(cctx, isn_type, type, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001099 else
1100 isn = generate_instr_drop(cctx, isn_type, 1);
1101 if (isn == NULL)
1102 return FAIL;
1103
1104 // This requires three arguments, which doesn't fit in an instruction, thus
1105 // we need to allocate a struct for this.
1106 sref = ALLOC_ONE(scriptref_T);
1107 if (sref == NULL)
1108 return FAIL;
1109 isn->isn_arg.script.scriptref = sref;
1110 sref->sref_sid = sid;
1111 sref->sref_idx = idx;
1112 sref->sref_seq = si->sn_script_seq;
1113 sref->sref_type = type;
1114 return OK;
1115}
1116
1117/*
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001118 * Generate an ISN_NEWLIST instruction for "count" items.
1119 * "use_null" is TRUE for null_list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001120 */
1121 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001122generate_NEWLIST(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001123{
1124 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001125 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001126 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001127 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001128
1129 RETURN_OK_IF_SKIP(cctx);
1130 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1131 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001132 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001133
Bram Moolenaar078a4612022-01-04 15:17:03 +00001134 // Get the member type and the declared member type from all the items on
1135 // the stack.
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001136 member_type = get_member_type_from_stack(count, 1, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001137 type = get_list_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001138 decl_type = get_list_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001139
1140 // drop the value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001141 cctx->ctx_type_stack.ga_len -= count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001142
1143 // add the list type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001144 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001145}
1146
1147/*
1148 * Generate an ISN_NEWDICT instruction.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001149 * "use_null" is TRUE for null_dict.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001150 */
1151 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001152generate_NEWDICT(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001153{
1154 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001155 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001156 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001157 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001158
1159 RETURN_OK_IF_SKIP(cctx);
1160 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1161 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001162 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001163
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001164 member_type = get_member_type_from_stack(count, 2, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001165 type = get_dict_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001166 decl_type = get_dict_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001167
1168 // drop the key and value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001169 cctx->ctx_type_stack.ga_len -= 2 * count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001170
1171 // add the dict type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001172 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001173}
1174
1175/*
1176 * Generate an ISN_FUNCREF instruction.
Bram Moolenaara915fa02022-03-23 11:29:15 +00001177 * "isnp" is set to the instruction, so that fr_dfunc_idx can be set later.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001178 */
1179 int
Bram Moolenaara915fa02022-03-23 11:29:15 +00001180generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc, isn_T **isnp)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001181{
1182 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001183 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001184
1185 RETURN_OK_IF_SKIP(cctx);
1186 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1187 return FAIL;
Bram Moolenaara915fa02022-03-23 11:29:15 +00001188 if (isnp != NULL)
1189 *isnp = isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001190 if (ufunc->uf_def_status == UF_NOT_COMPILED)
1191 isn->isn_arg.funcref.fr_func_name = vim_strsave(ufunc->uf_name);
1192 else
1193 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
1194 cctx->ctx_has_closure = 1;
1195
1196 // If the referenced function is a closure, it may use items further up in
Bram Moolenaar139575d2022-03-15 19:29:30 +00001197 // the nested context, including this one. But not a function defined at
1198 // the script level.
1199 if ((ufunc->uf_flags & FC_CLOSURE)
1200 && func_name_refcount(cctx->ctx_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001201 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1202
Bram Moolenaar078a4612022-01-04 15:17:03 +00001203 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1204 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001205}
1206
1207/*
1208 * Generate an ISN_NEWFUNC instruction.
1209 * "lambda_name" and "func_name" must be in allocated memory and will be
1210 * consumed.
1211 */
1212 int
1213generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name)
1214{
1215 isn_T *isn;
1216
1217 if (cctx->ctx_skip == SKIP_YES)
1218 {
1219 vim_free(lambda_name);
1220 vim_free(func_name);
1221 return OK;
1222 }
1223 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1224 {
1225 vim_free(lambda_name);
1226 vim_free(func_name);
1227 return FAIL;
1228 }
1229 isn->isn_arg.newfunc.nf_lambda = lambda_name;
1230 isn->isn_arg.newfunc.nf_global = func_name;
1231
1232 return OK;
1233}
1234
1235/*
1236 * Generate an ISN_DEF instruction: list functions
1237 */
1238 int
1239generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1240{
1241 isn_T *isn;
1242
1243 RETURN_OK_IF_SKIP(cctx);
1244 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1245 return FAIL;
1246 if (len > 0)
1247 {
1248 isn->isn_arg.string = vim_strnsave(name, len);
1249 if (isn->isn_arg.string == NULL)
1250 return FAIL;
1251 }
1252 return OK;
1253}
1254
1255/*
1256 * Generate an ISN_JUMP instruction.
1257 */
1258 int
1259generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1260{
1261 isn_T *isn;
1262 garray_T *stack = &cctx->ctx_type_stack;
1263
1264 RETURN_OK_IF_SKIP(cctx);
1265 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1266 return FAIL;
1267 isn->isn_arg.jump.jump_when = when;
1268 isn->isn_arg.jump.jump_where = where;
1269
1270 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1271 --stack->ga_len;
1272
1273 return OK;
1274}
1275
1276/*
1277 * Generate an ISN_JUMP_IF_ARG_SET instruction.
1278 */
1279 int
1280generate_JUMP_IF_ARG_SET(cctx_T *cctx, int arg_off)
1281{
1282 isn_T *isn;
1283
1284 RETURN_OK_IF_SKIP(cctx);
1285 if ((isn = generate_instr(cctx, ISN_JUMP_IF_ARG_SET)) == NULL)
1286 return FAIL;
1287 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1288 // jump_where is set later
1289 return OK;
1290}
1291
1292 int
1293generate_FOR(cctx_T *cctx, int loop_idx)
1294{
1295 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001296
1297 RETURN_OK_IF_SKIP(cctx);
1298 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1299 return FAIL;
1300 isn->isn_arg.forloop.for_idx = loop_idx;
1301
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001302 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001303 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001304}
1305/*
1306 * Generate an ISN_TRYCONT instruction.
1307 */
1308 int
1309generate_TRYCONT(cctx_T *cctx, int levels, int where)
1310{
1311 isn_T *isn;
1312
1313 RETURN_OK_IF_SKIP(cctx);
1314 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1315 return FAIL;
1316 isn->isn_arg.trycont.tct_levels = levels;
1317 isn->isn_arg.trycont.tct_where = where;
1318
1319 return OK;
1320}
1321
1322
1323/*
1324 * Generate an ISN_BCALL instruction.
1325 * "method_call" is TRUE for "value->method()"
1326 * Return FAIL if the number of arguments is wrong.
1327 */
1328 int
1329generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1330{
1331 isn_T *isn;
1332 garray_T *stack = &cctx->ctx_type_stack;
1333 int argoff;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001334 type2_T *typep;
1335 type2_T *argtypes = NULL;
1336 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1337 type2_T *maptype = NULL;
1338 type_T *type;
Bram Moolenaar81330182022-02-01 12:11:58 +00001339 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001340
1341 RETURN_OK_IF_SKIP(cctx);
1342 argoff = check_internal_func(func_idx, argcount);
1343 if (argoff < 0)
1344 return FAIL;
1345
1346 if (method_call && argoff > 1)
1347 {
1348 if ((isn = generate_instr(cctx, ISN_SHUFFLE)) == NULL)
1349 return FAIL;
1350 isn->isn_arg.shuffle.shfl_item = argcount;
1351 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1352 }
1353
1354 if (argcount > 0)
1355 {
1356 // Check the types of the arguments.
Bram Moolenaar078a4612022-01-04 15:17:03 +00001357 typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001358 if (method_call && argoff > 1)
1359 {
1360 int i;
1361
1362 for (i = 0; i < argcount; ++i)
1363 shuffled_argtypes[i] = (i < argoff - 1)
Bram Moolenaar078a4612022-01-04 15:17:03 +00001364 ? typep[i + 1]
1365 : (i == argoff - 1) ? typep[0] : typep[i];
1366 argtypes = shuffled_argtypes;
1367 }
1368 else
1369 {
1370 int i;
1371
1372 for (i = 0; i < argcount; ++i)
1373 shuffled_argtypes[i] = typep[i];
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001374 argtypes = shuffled_argtypes;
1375 }
1376 if (internal_func_check_arg_types(argtypes, func_idx, argcount,
1377 cctx) == FAIL)
1378 return FAIL;
1379 if (internal_func_is_map(func_idx))
Bram Moolenaar078a4612022-01-04 15:17:03 +00001380 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001381 }
1382
1383 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1384 return FAIL;
1385 isn->isn_arg.bfunc.cbf_idx = func_idx;
1386 isn->isn_arg.bfunc.cbf_argcount = argcount;
1387
1388 // Drop the argument types and push the return type.
1389 stack->ga_len -= argcount;
Bram Moolenaar81330182022-02-01 12:11:58 +00001390 type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type);
1391 if (push_type_stack2(cctx, type, decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001392 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001393
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001394 if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
1395 && maptype[0].type_decl->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001396 // Check that map() didn't change the item types.
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001397 generate_TYPECHECK(cctx, maptype[0].type_decl, -1, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001398
1399 return OK;
1400}
1401
1402/*
1403 * Generate an ISN_LISTAPPEND instruction. Works like add().
1404 * Argument count is already checked.
1405 */
1406 int
1407generate_LISTAPPEND(cctx_T *cctx)
1408{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001409 type_T *list_type;
1410 type_T *item_type;
1411 type_T *expected;
1412
1413 // Caller already checked that list_type is a list.
Bram Moolenaar81330182022-02-01 12:11:58 +00001414 // For checking the item type we use the declared type of the list and the
1415 // current type of the added item, adding a string to [1, 2] is OK.
1416 list_type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001417 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001418 expected = list_type->tt_member;
1419 if (need_type(item_type, expected, -1, 0, cctx, FALSE, FALSE) == FAIL)
1420 return FAIL;
1421
1422 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1423 return FAIL;
1424
Bram Moolenaar078a4612022-01-04 15:17:03 +00001425 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001426 return OK;
1427}
1428
1429/*
1430 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1431 * Argument count is already checked.
1432 */
1433 int
1434generate_BLOBAPPEND(cctx_T *cctx)
1435{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001436 type_T *item_type;
1437
1438 // Caller already checked that blob_type is a blob.
Bram Moolenaar078a4612022-01-04 15:17:03 +00001439 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001440 if (need_type(item_type, &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
1441 return FAIL;
1442
1443 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1444 return FAIL;
1445
Bram Moolenaar078a4612022-01-04 15:17:03 +00001446 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001447 return OK;
1448}
1449
1450/*
1451 * Generate an ISN_DCALL or ISN_UCALL instruction.
1452 * Return FAIL if the number of arguments is wrong.
1453 */
1454 int
1455generate_CALL(cctx_T *cctx, ufunc_T *ufunc, int pushed_argcount)
1456{
1457 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001458 int regular_args = ufunc->uf_args.ga_len;
1459 int argcount = pushed_argcount;
1460
1461 RETURN_OK_IF_SKIP(cctx);
1462 if (argcount > regular_args && !has_varargs(ufunc))
1463 {
1464 semsg(_(e_too_many_arguments_for_function_str),
1465 printable_func_name(ufunc));
1466 return FAIL;
1467 }
1468 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1469 {
1470 semsg(_(e_not_enough_arguments_for_function_str),
1471 printable_func_name(ufunc));
1472 return FAIL;
1473 }
1474
1475 if (ufunc->uf_def_status != UF_NOT_COMPILED
1476 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1477 {
1478 int i;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001479 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001480
1481 for (i = 0; i < argcount; ++i)
1482 {
1483 type_T *expected;
1484 type_T *actual;
1485
Bram Moolenaar078a4612022-01-04 15:17:03 +00001486 actual = get_type_on_stack(cctx, argcount - i - 1);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001487 if (actual->tt_type == VAR_SPECIAL
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001488 && i >= regular_args - ufunc->uf_def_args.ga_len)
1489 {
1490 // assume v:none used for default argument value
1491 continue;
1492 }
1493 if (i < regular_args)
1494 {
1495 if (ufunc->uf_arg_types == NULL)
1496 continue;
1497 expected = ufunc->uf_arg_types[i];
1498 }
1499 else if (ufunc->uf_va_type == NULL
1500 || ufunc->uf_va_type == &t_list_any)
1501 // possibly a lambda or "...: any"
1502 expected = &t_any;
1503 else
1504 expected = ufunc->uf_va_type->tt_member;
1505 if (need_type(actual, expected, -argcount + i, i + 1, cctx,
1506 TRUE, FALSE) == FAIL)
1507 {
1508 arg_type_mismatch(expected, actual, i + 1);
1509 return FAIL;
1510 }
1511 }
Bram Moolenaar139575d2022-03-15 19:29:30 +00001512 compile_type = get_compile_type(ufunc);
1513 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001514 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaar139575d2022-03-15 19:29:30 +00001515 compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001516 return FAIL;
1517 }
1518 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1519 {
1520 emsg_funcname(_(e_call_to_function_that_failed_to_compile_str),
1521 ufunc->uf_name);
1522 return FAIL;
1523 }
1524
1525 if ((isn = generate_instr(cctx,
1526 ufunc->uf_def_status != UF_NOT_COMPILED ? ISN_DCALL
1527 : ISN_UCALL)) == NULL)
1528 return FAIL;
1529 if (isn->isn_type == ISN_DCALL)
1530 {
1531 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1532 isn->isn_arg.dfunc.cdf_argcount = argcount;
1533 }
1534 else
1535 {
1536 // A user function may be deleted and redefined later, can't use the
1537 // ufunc pointer, need to look it up again at runtime.
1538 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1539 isn->isn_arg.ufunc.cuf_argcount = argcount;
1540 }
1541
Bram Moolenaar078a4612022-01-04 15:17:03 +00001542 // drop the argument types
1543 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001544
Bram Moolenaar078a4612022-01-04 15:17:03 +00001545 // add return type
1546 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001547}
1548
1549/*
1550 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1551 */
1552 int
1553generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1554{
1555 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001556
1557 RETURN_OK_IF_SKIP(cctx);
1558 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1559 return FAIL;
1560 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1561 isn->isn_arg.ufunc.cuf_argcount = argcount;
1562
Bram Moolenaar078a4612022-01-04 15:17:03 +00001563 // drop the argument types
1564 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001565
Bram Moolenaar078a4612022-01-04 15:17:03 +00001566 // add return value
1567 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001568}
1569
1570/*
1571 * Generate an ISN_PCALL instruction.
1572 * "type" is the type of the FuncRef.
1573 */
1574 int
1575generate_PCALL(
1576 cctx_T *cctx,
1577 int argcount,
1578 char_u *name,
1579 type_T *type,
1580 int at_top)
1581{
1582 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001583 type_T *ret_type;
1584
1585 RETURN_OK_IF_SKIP(cctx);
1586
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001587 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001588 ret_type = &t_any;
1589 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
1590 {
1591 if (type->tt_argcount != -1)
1592 {
1593 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1594
1595 if (argcount < type->tt_min_argcount - varargs)
1596 {
1597 semsg(_(e_not_enough_arguments_for_function_str), name);
1598 return FAIL;
1599 }
1600 if (!varargs && argcount > type->tt_argcount)
1601 {
1602 semsg(_(e_too_many_arguments_for_function_str), name);
1603 return FAIL;
1604 }
1605 if (type->tt_args != NULL)
1606 {
1607 int i;
1608
1609 for (i = 0; i < argcount; ++i)
1610 {
1611 int offset = -argcount + i - (at_top ? 0 : 1);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001612 type_T *actual = get_type_on_stack(cctx, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001613 type_T *expected;
1614
1615 if (varargs && i >= type->tt_argcount - 1)
1616 expected = type->tt_args[
1617 type->tt_argcount - 1]->tt_member;
1618 else if (i >= type->tt_min_argcount
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001619 && actual->tt_type == VAR_SPECIAL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001620 expected = &t_any;
1621 else
1622 expected = type->tt_args[i];
1623 if (need_type(actual, expected, offset, i + 1,
1624 cctx, TRUE, FALSE) == FAIL)
1625 {
1626 arg_type_mismatch(expected, actual, i + 1);
1627 return FAIL;
1628 }
1629 }
1630 }
1631 }
1632 ret_type = type->tt_member;
1633 if (ret_type == &t_unknown)
1634 // return type not known yet, use a runtime check
1635 ret_type = &t_any;
1636 }
1637 else
1638 {
1639 semsg(_(e_not_callable_type_str), name);
1640 return FAIL;
1641 }
1642
1643 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
1644 return FAIL;
1645 isn->isn_arg.pfunc.cpf_top = at_top;
1646 isn->isn_arg.pfunc.cpf_argcount = argcount;
1647
Bram Moolenaar078a4612022-01-04 15:17:03 +00001648 // drop the arguments and the funcref/partial
1649 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001650
Bram Moolenaar078a4612022-01-04 15:17:03 +00001651 // push the return value
1652 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001653
1654 // If partial is above the arguments it must be cleared and replaced with
1655 // the return value.
1656 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
1657 return FAIL;
1658
1659 return OK;
1660}
1661
1662/*
1663 * Generate an ISN_STRINGMEMBER instruction.
1664 */
1665 int
1666generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
1667{
1668 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001669 type_T *type;
1670
1671 RETURN_OK_IF_SKIP(cctx);
1672 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
1673 return FAIL;
1674 isn->isn_arg.string = vim_strnsave(name, len);
1675
1676 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00001677 type = get_type_on_stack(cctx, 0);
Bram Moolenaar59618fe2021-12-21 12:32:17 +00001678 if (type->tt_type != VAR_DICT && type != &t_any && type != &t_unknown)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001679 {
1680 char *tofree;
1681
1682 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
1683 name, type_name(type, &tofree));
1684 vim_free(tofree);
1685 return FAIL;
1686 }
1687 // change dict type to dict member type
1688 if (type->tt_type == VAR_DICT)
1689 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00001690 type_T *ntype = type->tt_member == &t_unknown
1691 ? &t_any : type->tt_member;
1692 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001693 }
1694
1695 return OK;
1696}
1697
1698/*
1699 * Generate an ISN_ECHO instruction.
1700 */
1701 int
1702generate_ECHO(cctx_T *cctx, int with_white, int count)
1703{
1704 isn_T *isn;
1705
1706 RETURN_OK_IF_SKIP(cctx);
1707 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
1708 return FAIL;
1709 isn->isn_arg.echo.echo_with_white = with_white;
1710 isn->isn_arg.echo.echo_count = count;
1711
1712 return OK;
1713}
1714
1715/*
1716 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
1717 */
1718 int
1719generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
1720{
1721 isn_T *isn;
1722
1723 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
1724 return FAIL;
1725 isn->isn_arg.number = count;
1726
1727 return OK;
1728}
1729
1730/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001731 * Generate an ISN_SOURCE instruction.
1732 */
1733 int
1734generate_SOURCE(cctx_T *cctx, int sid)
1735{
1736 isn_T *isn;
1737
1738 if ((isn = generate_instr(cctx, ISN_SOURCE)) == NULL)
1739 return FAIL;
1740 isn->isn_arg.number = sid;
1741
1742 return OK;
1743}
1744
1745/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001746 * Generate an ISN_PUT instruction.
1747 */
1748 int
1749generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
1750{
1751 isn_T *isn;
1752
1753 RETURN_OK_IF_SKIP(cctx);
1754 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
1755 return FAIL;
1756 isn->isn_arg.put.put_regname = regname;
1757 isn->isn_arg.put.put_lnum = lnum;
1758 return OK;
1759}
1760
1761/*
1762 * Generate an EXEC instruction that takes a string argument.
1763 * A copy is made of "line".
1764 */
1765 int
1766generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
1767{
1768 isn_T *isn;
1769
1770 RETURN_OK_IF_SKIP(cctx);
1771 if ((isn = generate_instr(cctx, isntype)) == NULL)
1772 return FAIL;
1773 isn->isn_arg.string = vim_strsave(line);
1774 return OK;
1775}
1776
1777/*
1778 * Generate an EXEC instruction that takes a string argument.
1779 * "str" must be allocated, it is consumed.
1780 */
1781 int
1782generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
1783{
1784 isn_T *isn;
1785
1786 if (cctx->ctx_skip == SKIP_YES)
1787 {
1788 vim_free(str);
1789 return OK;
1790 }
1791 if ((isn = generate_instr(cctx, isntype)) == NULL)
1792 {
1793 vim_free(str);
1794 return FAIL;
1795 }
1796 isn->isn_arg.string = str;
1797 return OK;
1798}
1799
1800 int
1801generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
1802{
1803 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001804
1805 RETURN_OK_IF_SKIP(cctx);
1806 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
1807 return FAIL;
1808 isn->isn_arg.string = vim_strsave(line);
1809
Bram Moolenaar078a4612022-01-04 15:17:03 +00001810 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001811}
1812
1813 int
1814generate_EXECCONCAT(cctx_T *cctx, int count)
1815{
1816 isn_T *isn;
1817
1818 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
1819 return FAIL;
1820 isn->isn_arg.number = count;
1821 return OK;
1822}
1823
1824/*
1825 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
1826 */
1827 int
1828generate_RANGE(cctx_T *cctx, char_u *range)
1829{
1830 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001831
1832 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
1833 return FAIL;
1834 isn->isn_arg.string = range;
1835
Bram Moolenaar078a4612022-01-04 15:17:03 +00001836 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001837}
1838
1839 int
1840generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
1841{
1842 isn_T *isn;
1843
1844 RETURN_OK_IF_SKIP(cctx);
1845 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
1846 return FAIL;
1847 isn->isn_arg.unpack.unp_count = var_count;
1848 isn->isn_arg.unpack.unp_semicolon = semicolon;
1849 return OK;
1850}
1851
1852/*
1853 * Generate an instruction for any command modifiers.
1854 */
1855 int
1856generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
1857{
1858 isn_T *isn;
1859
1860 if (has_cmdmod(cmod, FALSE))
1861 {
1862 cctx->ctx_has_cmdmod = TRUE;
1863
1864 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
1865 return FAIL;
1866 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
1867 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
1868 return FAIL;
1869 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
1870 // filter program now belongs to the instruction
1871 cmod->cmod_filter_regmatch.regprog = NULL;
1872 }
1873
1874 return OK;
1875}
1876
1877 int
1878generate_undo_cmdmods(cctx_T *cctx)
1879{
1880 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
1881 return FAIL;
1882 cctx->ctx_has_cmdmod = FALSE;
1883 return OK;
1884}
1885
1886/*
1887 * Generate a STORE instruction for "dest", not being "dest_local".
1888 * Return FAIL when out of memory.
1889 */
1890 int
1891generate_store_var(
1892 cctx_T *cctx,
1893 assign_dest_T dest,
1894 int opt_flags,
1895 int vimvaridx,
1896 int scriptvar_idx,
1897 int scriptvar_sid,
1898 type_T *type,
1899 char_u *name)
1900{
1901 switch (dest)
1902 {
1903 case dest_option:
1904 return generate_STOREOPT(cctx, ISN_STOREOPT,
1905 skip_option_env_lead(name), opt_flags);
1906 case dest_func_option:
1907 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
1908 skip_option_env_lead(name), opt_flags);
1909 case dest_global:
1910 // include g: with the name, easier to execute that way
1911 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
1912 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
1913 case dest_buffer:
1914 // include b: with the name, easier to execute that way
1915 return generate_STORE(cctx, ISN_STOREB, 0, name);
1916 case dest_window:
1917 // include w: with the name, easier to execute that way
1918 return generate_STORE(cctx, ISN_STOREW, 0, name);
1919 case dest_tab:
1920 // include t: with the name, easier to execute that way
1921 return generate_STORE(cctx, ISN_STORET, 0, name);
1922 case dest_env:
1923 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
1924 case dest_reg:
1925 return generate_STORE(cctx, ISN_STOREREG,
1926 name[1] == '@' ? '"' : name[1], NULL);
1927 case dest_vimvar:
1928 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
1929 case dest_script:
1930 if (scriptvar_idx < 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001931 {
1932 isntype_T isn_type = ISN_STORES;
1933
1934 if (SCRIPT_ID_VALID(scriptvar_sid)
Bram Moolenaarccbfd482022-03-31 16:18:23 +01001935 && SCRIPT_ITEM(scriptvar_sid)->sn_import_autoload
1936 && SCRIPT_ITEM(scriptvar_sid)->sn_autoload_prefix
1937 == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001938 {
1939 // "import autoload './dir/script.vim'" - load script first
1940 if (generate_SOURCE(cctx, scriptvar_sid) == FAIL)
1941 return FAIL;
1942 isn_type = ISN_STOREEXPORT;
1943 }
1944
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001945 // "s:" may be included in the name.
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001946 return generate_OLDSCRIPT(cctx, isn_type, name,
1947 scriptvar_sid, type);
1948 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001949 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
1950 scriptvar_sid, scriptvar_idx, type);
1951 case dest_local:
1952 case dest_expr:
1953 // cannot happen
1954 break;
1955 }
1956 return FAIL;
1957}
1958
Bram Moolenaar38ecd972022-01-15 21:44:44 +00001959/*
1960 * Return TRUE when inside a "for" or "while" loop.
1961 */
1962 int
1963inside_loop_scope(cctx_T *cctx)
1964{
1965 scope_T *scope = cctx->ctx_scope;
1966
1967 for (;;)
1968 {
1969 if (scope == NULL)
1970 break;
1971 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
1972 return TRUE;
1973 scope = scope->se_outer;
1974 }
1975 return FALSE;
1976}
1977
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001978 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001979generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001980{
1981 if (lhs->lhs_dest != dest_local)
1982 return generate_store_var(cctx, lhs->lhs_dest,
1983 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
1984 lhs->lhs_scriptvar_idx, lhs->lhs_scriptvar_sid,
1985 lhs->lhs_type, lhs->lhs_name);
1986
1987 if (lhs->lhs_lvar != NULL)
1988 {
1989 garray_T *instr = &cctx->ctx_instr;
1990 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
1991
Bram Moolenaar5cd64792021-12-25 18:23:24 +00001992 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
1993 // ISN_STORENR.
1994 // And "var = 0" does not need any instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001995 if (lhs->lhs_lvar->lv_from_outer == 0
1996 && instr->ga_len == instr_count + 1
1997 && isn->isn_type == ISN_PUSHNR)
1998 {
1999 varnumber_T val = isn->isn_arg.number;
2000 garray_T *stack = &cctx->ctx_type_stack;
2001
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002002 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002003 {
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002004 // zero is the default value, no need to do anything
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002005 --instr->ga_len;
2006 }
2007 else
2008 {
2009 isn->isn_type = ISN_STORENR;
2010 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
2011 isn->isn_arg.storenr.stnr_val = val;
2012 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002013 if (stack->ga_len > 0)
2014 --stack->ga_len;
2015 }
2016 else if (lhs->lhs_lvar->lv_from_outer > 0)
2017 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
2018 lhs->lhs_lvar->lv_from_outer);
2019 else
2020 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
2021 }
2022 return OK;
2023}
2024
2025#if defined(FEAT_PROFILE) || defined(PROTO)
2026 void
2027may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2028{
2029 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
2030 generate_instr(cctx, ISN_PROF_END);
2031}
2032#endif
2033
2034
2035/*
2036 * Delete an instruction, free what it contains.
2037 */
2038 void
2039delete_instr(isn_T *isn)
2040{
2041 switch (isn->isn_type)
2042 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002043 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002044 case ISN_DEF:
2045 case ISN_EXEC:
2046 case ISN_EXECRANGE:
2047 case ISN_EXEC_SPLIT:
2048 case ISN_LEGACY_EVAL:
2049 case ISN_LOADAUTO:
2050 case ISN_LOADB:
2051 case ISN_LOADENV:
2052 case ISN_LOADG:
2053 case ISN_LOADOPT:
2054 case ISN_LOADT:
2055 case ISN_LOADW:
2056 case ISN_LOCKUNLOCK:
2057 case ISN_PUSHEXC:
2058 case ISN_PUSHFUNC:
2059 case ISN_PUSHS:
2060 case ISN_RANGE:
2061 case ISN_STOREAUTO:
2062 case ISN_STOREB:
2063 case ISN_STOREENV:
2064 case ISN_STOREG:
2065 case ISN_STORET:
2066 case ISN_STOREW:
2067 case ISN_STRINGMEMBER:
2068 vim_free(isn->isn_arg.string);
2069 break;
2070
2071 case ISN_SUBSTITUTE:
2072 {
2073 int idx;
2074 isn_T *list = isn->isn_arg.subs.subs_instr;
2075
2076 vim_free(isn->isn_arg.subs.subs_cmd);
2077 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2078 delete_instr(list + idx);
2079 vim_free(list);
2080 }
2081 break;
2082
2083 case ISN_INSTR:
2084 {
2085 int idx;
2086 isn_T *list = isn->isn_arg.instr;
2087
2088 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2089 delete_instr(list + idx);
2090 vim_free(list);
2091 }
2092 break;
2093
2094 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002095 case ISN_LOADEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002096 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002097 case ISN_STOREEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002098 vim_free(isn->isn_arg.loadstore.ls_name);
2099 break;
2100
2101 case ISN_UNLET:
2102 case ISN_UNLETENV:
2103 vim_free(isn->isn_arg.unlet.ul_name);
2104 break;
2105
2106 case ISN_STOREOPT:
2107 case ISN_STOREFUNCOPT:
2108 vim_free(isn->isn_arg.storeopt.so_name);
2109 break;
2110
2111 case ISN_PUSHBLOB: // push blob isn_arg.blob
2112 blob_unref(isn->isn_arg.blob);
2113 break;
2114
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002115 case ISN_UCALL:
2116 vim_free(isn->isn_arg.ufunc.cuf_name);
2117 break;
2118
2119 case ISN_FUNCREF:
2120 {
2121 if (isn->isn_arg.funcref.fr_func_name == NULL)
2122 {
2123 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002124 + isn->isn_arg.funcref.fr_dfunc_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002125 ufunc_T *ufunc = dfunc->df_ufunc;
2126
2127 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2128 func_ptr_unref(ufunc);
2129 }
2130 else
2131 {
2132 char_u *name = isn->isn_arg.funcref.fr_func_name;
2133
2134 if (name != NULL)
2135 func_unref(name);
2136 vim_free(isn->isn_arg.funcref.fr_func_name);
2137 }
2138 }
2139 break;
2140
2141 case ISN_DCALL:
2142 {
2143 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002144 + isn->isn_arg.dfunc.cdf_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002145
2146 if (dfunc->df_ufunc != NULL
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002147 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002148 func_ptr_unref(dfunc->df_ufunc);
2149 }
2150 break;
2151
2152 case ISN_NEWFUNC:
2153 {
2154 char_u *lambda = isn->isn_arg.newfunc.nf_lambda;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002155 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002156
2157 if (ufunc != NULL)
2158 {
2159 unlink_def_function(ufunc);
2160 func_ptr_unref(ufunc);
2161 }
2162
2163 vim_free(lambda);
2164 vim_free(isn->isn_arg.newfunc.nf_global);
2165 }
2166 break;
2167
2168 case ISN_CHECKTYPE:
2169 case ISN_SETTYPE:
2170 free_type(isn->isn_arg.type.ct_type);
2171 break;
2172
2173 case ISN_CMDMOD:
2174 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002175 ->cmod_filter_regmatch.regprog);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002176 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2177 break;
2178
2179 case ISN_LOADSCRIPT:
2180 case ISN_STORESCRIPT:
2181 vim_free(isn->isn_arg.script.scriptref);
2182 break;
2183
2184 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002185 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002186 break;
2187
2188 case ISN_CEXPR_CORE:
2189 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2190 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2191 break;
2192
2193 case ISN_2BOOL:
2194 case ISN_2STRING:
2195 case ISN_2STRING_ANY:
2196 case ISN_ADDBLOB:
2197 case ISN_ADDLIST:
2198 case ISN_ANYINDEX:
2199 case ISN_ANYSLICE:
2200 case ISN_BCALL:
2201 case ISN_BLOBAPPEND:
2202 case ISN_BLOBINDEX:
2203 case ISN_BLOBSLICE:
2204 case ISN_CATCH:
2205 case ISN_CEXPR_AUCMD:
2206 case ISN_CHECKLEN:
2207 case ISN_CHECKNR:
2208 case ISN_CLEARDICT:
2209 case ISN_CMDMOD_REV:
2210 case ISN_COMPAREANY:
2211 case ISN_COMPAREBLOB:
2212 case ISN_COMPAREBOOL:
2213 case ISN_COMPAREDICT:
2214 case ISN_COMPAREFLOAT:
2215 case ISN_COMPAREFUNC:
2216 case ISN_COMPARELIST:
2217 case ISN_COMPARENR:
Bram Moolenaar7a222242022-03-01 19:23:24 +00002218 case ISN_COMPARENULL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002219 case ISN_COMPARESPECIAL:
2220 case ISN_COMPARESTRING:
2221 case ISN_CONCAT:
2222 case ISN_COND2BOOL:
2223 case ISN_DEBUG:
2224 case ISN_DROP:
2225 case ISN_ECHO:
2226 case ISN_ECHOCONSOLE:
2227 case ISN_ECHOERR:
2228 case ISN_ECHOMSG:
2229 case ISN_ENDTRY:
2230 case ISN_EXECCONCAT:
2231 case ISN_EXECUTE:
2232 case ISN_FINALLY:
2233 case ISN_FINISH:
2234 case ISN_FOR:
2235 case ISN_GETITEM:
2236 case ISN_JUMP:
2237 case ISN_JUMP_IF_ARG_SET:
2238 case ISN_LISTAPPEND:
2239 case ISN_LISTINDEX:
2240 case ISN_LISTSLICE:
2241 case ISN_LOAD:
2242 case ISN_LOADBDICT:
2243 case ISN_LOADGDICT:
2244 case ISN_LOADOUTER:
2245 case ISN_LOADREG:
2246 case ISN_LOADTDICT:
2247 case ISN_LOADV:
2248 case ISN_LOADWDICT:
2249 case ISN_LOCKCONST:
2250 case ISN_MEMBER:
2251 case ISN_NEGATENR:
2252 case ISN_NEWDICT:
2253 case ISN_NEWLIST:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002254 case ISN_NEWPARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002255 case ISN_OPANY:
2256 case ISN_OPFLOAT:
2257 case ISN_OPNR:
2258 case ISN_PCALL:
2259 case ISN_PCALL_END:
2260 case ISN_PROF_END:
2261 case ISN_PROF_START:
2262 case ISN_PUSHBOOL:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002263 case ISN_PUSHCHANNEL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002264 case ISN_PUSHF:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002265 case ISN_PUSHJOB:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002266 case ISN_PUSHNR:
2267 case ISN_PUSHSPEC:
2268 case ISN_PUT:
2269 case ISN_REDIREND:
2270 case ISN_REDIRSTART:
2271 case ISN_RETURN:
2272 case ISN_RETURN_VOID:
2273 case ISN_SHUFFLE:
2274 case ISN_SLICE:
2275 case ISN_STORE:
2276 case ISN_STOREINDEX:
2277 case ISN_STORENR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002278 case ISN_SOURCE:
2279 case ISN_STOREOUTER:
2280 case ISN_STORERANGE:
2281 case ISN_STOREREG:
2282 case ISN_STOREV:
2283 case ISN_STRINDEX:
2284 case ISN_STRSLICE:
2285 case ISN_THROW:
2286 case ISN_TRYCONT:
2287 case ISN_UNLETINDEX:
2288 case ISN_UNLETRANGE:
2289 case ISN_UNPACK:
2290 case ISN_USEDICT:
2291 // nothing allocated
2292 break;
2293}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002294}
2295
2296 void
2297clear_instr_ga(garray_T *gap)
2298{
2299 int idx;
2300
2301 for (idx = 0; idx < gap->ga_len; ++idx)
2302 delete_instr(((isn_T *)gap->ga_data) + idx);
2303 ga_clear(gap);
2304}
2305
2306
2307#endif // defined(FEAT_EVAL)