blob: 697c83d8ae6cb3dfd9fd2f64b46e09eed7038a8c [file] [log] [blame]
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9instr.c: Dealing with instructions of a compiled function
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19// When not generating protos this is included in proto.h
20#ifdef PROTO
21# include "vim9.h"
22#endif
23
24
25/////////////////////////////////////////////////////////////////////
26// Following generate_ functions expect the caller to call ga_grow().
27
28#define RETURN_NULL_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return NULL
29#define RETURN_OK_IF_SKIP(cctx) if (cctx->ctx_skip == SKIP_YES) return OK
30
31/*
32 * Generate an instruction without arguments.
33 * Returns a pointer to the new instruction, NULL if failed.
34 */
35 isn_T *
36generate_instr(cctx_T *cctx, isntype_T isn_type)
37{
38 garray_T *instr = &cctx->ctx_instr;
39 isn_T *isn;
40
41 RETURN_NULL_IF_SKIP(cctx);
42 if (GA_GROW_FAILS(instr, 1))
43 return NULL;
44 isn = ((isn_T *)instr->ga_data) + instr->ga_len;
45 isn->isn_type = isn_type;
46 isn->isn_lnum = cctx->ctx_lnum + 1;
47 ++instr->ga_len;
48
49 return isn;
50}
51
52/*
53 * Generate an instruction without arguments.
54 * "drop" will be removed from the stack.
55 * Returns a pointer to the new instruction, NULL if failed.
56 */
57 isn_T *
58generate_instr_drop(cctx_T *cctx, isntype_T isn_type, int drop)
59{
Bram Moolenaardc7c3662021-12-20 15:04:29 +000060 RETURN_NULL_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +000061 cctx->ctx_type_stack.ga_len -= drop;
Bram Moolenaardc7c3662021-12-20 15:04:29 +000062 return generate_instr(cctx, isn_type);
63}
64
65/*
Bram Moolenaar078a4612022-01-04 15:17:03 +000066 * Generate instruction "isn_type" and put "type" on the type stack,
67 * use "decl_type" for the declared type.
Bram Moolenaardc7c3662021-12-20 15:04:29 +000068 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +000069 static isn_T *
Bram Moolenaar078a4612022-01-04 15:17:03 +000070generate_instr_type2(
71 cctx_T *cctx,
72 isntype_T isn_type,
73 type_T *type,
74 type_T *decl_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +000075{
76 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +000077
78 if ((isn = generate_instr(cctx, isn_type)) == NULL)
79 return NULL;
80
Bram Moolenaar078a4612022-01-04 15:17:03 +000081 if (push_type_stack2(cctx, type == NULL ? &t_any : type,
82 decl_type == NULL ? &t_any : decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +000083 return NULL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +000084
85 return isn;
86}
87
88/*
Bram Moolenaar078a4612022-01-04 15:17:03 +000089 * Generate instruction "isn_type" and put "type" on the type stack.
90 * Uses "any" for the declared type, which works for constants. For declared
91 * variables use generate_instr_type2().
92 */
93 isn_T *
94generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type)
95{
96 return generate_instr_type2(cctx, isn_type, type, &t_any);
97}
98
99/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000100 * Generate an ISN_DEBUG instruction.
101 */
102 isn_T *
103generate_instr_debug(cctx_T *cctx)
104{
105 isn_T *isn;
106 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
107 + cctx->ctx_ufunc->uf_dfunc_idx;
108
109 if ((isn = generate_instr(cctx, ISN_DEBUG)) == NULL)
110 return NULL;
111 isn->isn_arg.debug.dbg_var_names_len = dfunc->df_var_names.ga_len;
112 isn->isn_arg.debug.dbg_break_lnum = cctx->ctx_prev_lnum;
113 return isn;
114}
115
116/*
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000117 * Generate an ISN_CONSTRUCT instruction.
118 * The object will have "size" members.
119 */
120 int
121generate_CONSTRUCT(cctx_T *cctx, class_T *cl)
122{
123 isn_T *isn;
124
125 RETURN_OK_IF_SKIP(cctx);
126 if ((isn = generate_instr(cctx, ISN_CONSTRUCT)) == NULL)
127 return FAIL;
128 isn->isn_arg.construct.construct_size = sizeof(object_T)
129 + cl->class_obj_member_count * sizeof(typval_T);
130 isn->isn_arg.construct.construct_class = cl;
131 return OK;
132}
133
134/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000135 * Generate ISN_GET_OBJ_MEMBER - access member of object at bottom of stack by
136 * index.
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000137 */
138 int
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +0200139generate_GET_OBJ_MEMBER(cctx_T *cctx, int idx, type_T *type)
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000140{
141 RETURN_OK_IF_SKIP(cctx);
142
143 // drop the object type
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000144 isn_T *isn = generate_instr_drop(cctx, ISN_GET_OBJ_MEMBER, 1);
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000145 if (isn == NULL)
146 return FAIL;
147
Ernie Rael18143d32023-09-04 22:30:41 +0200148 isn->isn_arg.classmember.cm_class = NULL;
149 isn->isn_arg.classmember.cm_idx = idx;
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000150 return push_type_stack2(cctx, type, &t_any);
151}
152
153/*
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000154 * Generate ISN_GET_ITF_MEMBER - access member of interface at bottom of stack
155 * by index.
156 */
157 int
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +0200158generate_GET_ITF_MEMBER(cctx_T *cctx, class_T *itf, int idx, type_T *type)
Bram Moolenaar29ac5df2023-01-16 19:43:47 +0000159{
160 RETURN_OK_IF_SKIP(cctx);
161
162 // drop the object type
163 isn_T *isn = generate_instr_drop(cctx, ISN_GET_ITF_MEMBER, 1);
164 if (isn == NULL)
165 return FAIL;
166
167 isn->isn_arg.classmember.cm_class = itf;
168 ++itf->class_refcount;
169 isn->isn_arg.classmember.cm_idx = idx;
170 return push_type_stack2(cctx, type, &t_any);
171}
172
173/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +0000174 * Generate ISN_STORE_THIS - store value in member of "this" object with member
175 * index "idx".
176 */
177 int
178generate_STORE_THIS(cctx_T *cctx, int idx)
179{
180 RETURN_OK_IF_SKIP(cctx);
181
182 // drop the value type
183 isn_T *isn = generate_instr_drop(cctx, ISN_STORE_THIS, 1);
184 if (isn == NULL)
185 return FAIL;
186
187 isn->isn_arg.number = idx;
188 return OK;
189}
190
191/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000192 * If type at "offset" isn't already VAR_STRING then generate ISN_2STRING.
193 * But only for simple types.
194 * When "tolerant" is TRUE convert most types to string, e.g. a List.
195 */
196 int
197may_generate_2STRING(int offset, int tolerant, cctx_T *cctx)
198{
199 isn_T *isn;
200 isntype_T isntype = ISN_2STRING;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000201 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000202
203 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +0000204 type = get_type_on_stack(cctx, -1 - offset);
205 switch (type->tt_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000206 {
207 // nothing to be done
208 case VAR_STRING: return OK;
209
210 // conversion possible
211 case VAR_SPECIAL:
212 case VAR_BOOL:
213 case VAR_NUMBER:
214 case VAR_FLOAT:
215 break;
216
217 // conversion possible (with runtime check)
218 case VAR_ANY:
219 case VAR_UNKNOWN:
220 isntype = ISN_2STRING_ANY;
221 break;
222
223 // conversion possible when tolerant
224 case VAR_LIST:
225 if (tolerant)
226 {
227 isntype = ISN_2STRING_ANY;
228 break;
229 }
230 // FALLTHROUGH
231
232 // conversion not possible
233 case VAR_VOID:
234 case VAR_BLOB:
235 case VAR_FUNC:
236 case VAR_PARTIAL:
237 case VAR_DICT:
238 case VAR_JOB:
239 case VAR_CHANNEL:
240 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000241 case VAR_CLASS:
242 case VAR_OBJECT:
Bram Moolenaar078a4612022-01-04 15:17:03 +0000243 to_string_error(type->tt_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000244 return FAIL;
245 }
246
Bram Moolenaar078a4612022-01-04 15:17:03 +0000247 set_type_on_stack(cctx, &t_string, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000248 if ((isn = generate_instr(cctx, isntype)) == NULL)
249 return FAIL;
250 isn->isn_arg.tostring.offset = offset;
251 isn->isn_arg.tostring.tolerant = tolerant;
252
253 return OK;
254}
255
256 static int
257check_number_or_float(vartype_T type1, vartype_T type2, char_u *op)
258{
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000259 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT
260 || type1 == VAR_ANY || type1 == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000261 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000262 || type2 == VAR_ANY || type2 == VAR_UNKNOWN)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000263 {
264 if (*op == '+')
265 emsg(_(e_wrong_argument_type_for_plus));
266 else
267 semsg(_(e_char_requires_number_or_float_arguments), *op);
268 return FAIL;
269 }
270 return OK;
271}
272
273/*
274 * Generate instruction for "+". For a list this creates a new list.
275 */
276 int
277generate_add_instr(
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000278 cctx_T *cctx,
279 vartype_T vartype,
280 type_T *type1,
281 type_T *type2,
282 exprtype_T expr_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000283{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000284 isn_T *isn = generate_instr_drop(cctx,
285 vartype == VAR_NUMBER ? ISN_OPNR
286 : vartype == VAR_LIST ? ISN_ADDLIST
287 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000288 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000289 : ISN_OPANY, 1);
290
291 if (vartype != VAR_LIST && vartype != VAR_BLOB
292 && type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000293 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000294 && type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000295 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000296 && check_number_or_float(
297 type1->tt_type, type2->tt_type, (char_u *)"+") == FAIL)
298 return FAIL;
299
300 if (isn != NULL)
301 {
302 if (isn->isn_type == ISN_ADDLIST)
303 isn->isn_arg.op.op_type = expr_type;
304 else
305 isn->isn_arg.op.op_type = EXPR_ADD;
306 }
307
308 // When concatenating two lists with different member types the member type
309 // becomes "any".
310 if (vartype == VAR_LIST
311 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
312 && type1->tt_member != type2->tt_member)
Bram Moolenaar078a4612022-01-04 15:17:03 +0000313 set_type_on_stack(cctx, &t_list_any, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000314
315 return isn == NULL ? FAIL : OK;
316}
317
318/*
319 * Get the type to use for an instruction for an operation on "type1" and
320 * "type2". If they are matching use a type-specific instruction. Otherwise
321 * fall back to runtime type checking.
322 */
323 vartype_T
324operator_type(type_T *type1, type_T *type2)
325{
326 if (type1->tt_type == type2->tt_type
327 && (type1->tt_type == VAR_NUMBER
328 || type1->tt_type == VAR_LIST
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000329 || type1->tt_type == VAR_FLOAT
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000330 || type1->tt_type == VAR_BLOB))
331 return type1->tt_type;
332 return VAR_ANY;
333}
334
335/*
336 * Generate an instruction with two arguments. The instruction depends on the
337 * type of the arguments.
338 */
339 int
340generate_two_op(cctx_T *cctx, char_u *op)
341{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000342 type_T *type1;
343 type_T *type2;
344 vartype_T vartype;
345 isn_T *isn;
346
347 RETURN_OK_IF_SKIP(cctx);
348
349 // Get the known type of the two items on the stack.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000350 type1 = get_type_on_stack(cctx, 1);
351 type2 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000352 vartype = operator_type(type1, type2);
353
354 switch (*op)
355 {
356 case '+':
357 if (generate_add_instr(cctx, vartype, type1, type2,
358 EXPR_COPY) == FAIL)
359 return FAIL;
360 break;
361
362 case '-':
363 case '*':
364 case '/': if (check_number_or_float(type1->tt_type, type2->tt_type,
365 op) == FAIL)
366 return FAIL;
367 if (vartype == VAR_NUMBER)
368 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000369 else if (vartype == VAR_FLOAT)
370 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000371 else
372 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
373 if (isn != NULL)
374 isn->isn_arg.op.op_type = *op == '*'
375 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
376 break;
377
378 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000379 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000380 && type1->tt_type != VAR_NUMBER)
381 || (type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000382 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000383 && type2->tt_type != VAR_NUMBER))
384 {
385 emsg(_(e_percent_requires_number_arguments));
386 return FAIL;
387 }
388 isn = generate_instr_drop(cctx,
389 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
390 if (isn != NULL)
391 isn->isn_arg.op.op_type = EXPR_REM;
392 break;
393 }
394
395 // correct type of result
396 if (vartype == VAR_ANY)
397 {
398 type_T *type = &t_any;
399
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000400 // float+number and number+float results in float
401 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100402 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000403 type = &t_float;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000404 set_type_on_stack(cctx, type, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000405 }
406
407 return OK;
408}
409
410/*
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000411 * Get the instruction to use for comparing two values with specified types.
412 * Either "tv1" and "tv2" are passed or "type1" and "type2".
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000413 * Return ISN_DROP when failed.
414 */
415 static isntype_T
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000416get_compare_isn(
Bram Moolenaar2ed57ac2023-04-01 22:05:38 +0100417 exprtype_T exprtype,
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000418 typval_T *tv1,
419 typval_T *tv2,
420 type_T *type1,
421 type_T *type2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000422{
423 isntype_T isntype = ISN_DROP;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000424 vartype_T vartype1 = tv1 != NULL ? tv1->v_type : type1->tt_type;
425 vartype_T vartype2 = tv2 != NULL ? tv2->v_type : type2->tt_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000426
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000427 if (vartype1 == vartype2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000428 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000429 switch (vartype1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000430 {
431 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
432 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
433 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
434 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
435 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
436 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
437 case VAR_LIST: isntype = ISN_COMPARELIST; break;
438 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
439 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000440 case VAR_CLASS: isntype = ISN_COMPARECLASS; break;
441 case VAR_OBJECT: isntype = ISN_COMPAREOBJECT; break;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000442 default: isntype = ISN_COMPAREANY; break;
443 }
444 }
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000445 else if (vartype1 == VAR_ANY || vartype2 == VAR_ANY
446 || ((vartype1 == VAR_NUMBER || vartype1 == VAR_FLOAT)
447 && (vartype2 == VAR_NUMBER || vartype2 == VAR_FLOAT))
448 || (vartype1 == VAR_FUNC && vartype2 == VAR_PARTIAL)
449 || (vartype1 == VAR_PARTIAL && vartype2 == VAR_FUNC))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000450 isntype = ISN_COMPAREANY;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000451 else if (vartype1 == VAR_SPECIAL || vartype2 == VAR_SPECIAL)
Bram Moolenaar7a222242022-03-01 19:23:24 +0000452 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000453 if ((vartype1 == VAR_SPECIAL
454 && (tv1 != NULL ? tv1->vval.v_number == VVAL_NONE
455 : type1 == &t_none)
456 && vartype2 != VAR_STRING)
457 || (vartype2 == VAR_SPECIAL
458 && (tv2 != NULL ? tv2->vval.v_number == VVAL_NONE
459 : type2 == &t_none)
460 && vartype1 != VAR_STRING))
461 {
462 semsg(_(e_cannot_compare_str_with_str),
463 vartype_name(vartype1), vartype_name(vartype2));
464 return ISN_DROP;
465 }
Bram Moolenaar05667812022-03-15 20:21:33 +0000466 // although comparing null with number, float or bool is not useful, we
467 // allow it
Bram Moolenaar7a222242022-03-01 19:23:24 +0000468 isntype = ISN_COMPARENULL;
469 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000470
471 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
472 && (isntype == ISN_COMPAREBOOL
473 || isntype == ISN_COMPARESPECIAL
474 || isntype == ISN_COMPARENR
475 || isntype == ISN_COMPAREFLOAT))
476 {
477 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000478 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(vartype1));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000479 return ISN_DROP;
480 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000481 if (!(exprtype == EXPR_IS || exprtype == EXPR_ISNOT
482 || exprtype == EXPR_EQUAL || exprtype == EXPR_NEQUAL)
483 && (isntype == ISN_COMPAREOBJECT || isntype == ISN_COMPARECLASS))
484 {
485 semsg(_(e_invalid_operation_for_str), vartype_name(vartype1));
486 return ISN_DROP;
487 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000488 if (isntype == ISN_DROP
Bram Moolenaar2ed57ac2023-04-01 22:05:38 +0100489 || (isntype != ISN_COMPARENULL
490 && (((exprtype != EXPR_EQUAL
491 && exprtype != EXPR_NEQUAL
492 && (vartype1 == VAR_BOOL || vartype1 == VAR_SPECIAL
493 || vartype2 == VAR_BOOL || vartype2 == VAR_SPECIAL)))
494 || ((exprtype != EXPR_EQUAL
495 && exprtype != EXPR_NEQUAL
496 && exprtype != EXPR_IS
497 && exprtype != EXPR_ISNOT
498 && (vartype1 == VAR_BLOB || vartype2 == VAR_BLOB
499 || vartype1 == VAR_LIST || vartype2 == VAR_LIST))))))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000500 {
501 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000502 vartype_name(vartype1), vartype_name(vartype2));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000503 return ISN_DROP;
504 }
505 return isntype;
506}
507
508 int
509check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
510{
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000511 if (get_compare_isn(type, tv1, tv2, NULL, NULL) == ISN_DROP)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000512 return FAIL;
513 return OK;
514}
515
516/*
517 * Generate an ISN_COMPARE* instruction with a boolean result.
518 */
519 int
520generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
521{
522 isntype_T isntype;
523 isn_T *isn;
524 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000525
526 RETURN_OK_IF_SKIP(cctx);
527
528 // Get the known type of the two items on the stack. If they are matching
529 // use a type-specific instruction. Otherwise fall back to runtime type
530 // checking.
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000531 isntype = get_compare_isn(exprtype, NULL, NULL,
532 get_type_on_stack(cctx, 1), get_type_on_stack(cctx, 0));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000533 if (isntype == ISN_DROP)
534 return FAIL;
535
536 if ((isn = generate_instr(cctx, isntype)) == NULL)
537 return FAIL;
538 isn->isn_arg.op.op_type = exprtype;
539 isn->isn_arg.op.op_ic = ic;
540
541 // takes two arguments, puts one bool back
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100542 --stack->ga_len;
543 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000544
545 return OK;
546}
547
548/*
LemonBoy372bcce2022-04-25 12:43:20 +0100549 * Generate an ISN_CONCAT instruction.
550 * "count" is the number of stack elements to join together and it must be
551 * greater or equal to one.
552 * The caller ensures all the "count" elements on the stack have the right type.
553 */
554 int
555generate_CONCAT(cctx_T *cctx, int count)
556{
557 isn_T *isn;
558 garray_T *stack = &cctx->ctx_type_stack;
559
560 RETURN_OK_IF_SKIP(cctx);
561
LemonBoy372bcce2022-04-25 12:43:20 +0100562 if ((isn = generate_instr(cctx, ISN_CONCAT)) == NULL)
563 return FAIL;
564 isn->isn_arg.number = count;
565
566 // drop the argument types
567 stack->ga_len -= count - 1;
568
569 return OK;
570}
571
572/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000573 * Generate an ISN_2BOOL instruction.
574 * "offset" is the offset in the type stack.
575 */
576 int
577generate_2BOOL(cctx_T *cctx, int invert, int offset)
578{
579 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000580
581 RETURN_OK_IF_SKIP(cctx);
582 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
583 return FAIL;
584 isn->isn_arg.tobool.invert = invert;
585 isn->isn_arg.tobool.offset = offset;
586
587 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000588 set_type_on_stack(cctx, &t_bool, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000589
590 return OK;
591}
592
593/*
594 * Generate an ISN_COND2BOOL instruction.
595 */
596 int
597generate_COND2BOOL(cctx_T *cctx)
598{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000599 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100600 if (generate_instr(cctx, ISN_COND2BOOL) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000601 return FAIL;
602
603 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000604 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000605
606 return OK;
607}
608
609 int
610generate_TYPECHECK(
611 cctx_T *cctx,
612 type_T *expected,
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000613 int number_ok, // add TTFLAG_NUMBER_OK flag
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000614 int offset,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100615 int is_var,
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000616 int argidx)
617{
618 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000619
620 RETURN_OK_IF_SKIP(cctx);
621 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
622 return FAIL;
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000623 type_T *tt;
624 if (expected->tt_type == VAR_FLOAT && number_ok)
625 {
626 // always allocate, also for static types
627 tt = ALLOC_ONE(type_T);
628 if (tt != NULL)
629 {
630 *tt = *expected;
Bram Moolenaarc4b3f642022-12-30 10:36:34 +0000631 tt->tt_flags &= ~TTFLAG_STATIC;
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000632 tt->tt_flags |= TTFLAG_NUMBER_OK;
633 }
634 }
635 else
636 tt = alloc_type(expected);
637
638 isn->isn_arg.type.ct_type = tt;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000639 isn->isn_arg.type.ct_off = (int8_T)offset;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100640 isn->isn_arg.type.ct_is_var = is_var;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000641 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
642
643 // type becomes expected
Bram Moolenaar078a4612022-01-04 15:17:03 +0000644 set_type_on_stack(cctx, expected, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000645
646 return OK;
647}
648
649 int
650generate_SETTYPE(
651 cctx_T *cctx,
652 type_T *expected)
653{
654 isn_T *isn;
655
656 RETURN_OK_IF_SKIP(cctx);
657 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
658 return FAIL;
659 isn->isn_arg.type.ct_type = alloc_type(expected);
660 return OK;
661}
662
663/*
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000664 * Generate an ISN_PUSHOBJ instruction. Object is always NULL.
665 */
Ernie Rael5c018be2023-08-27 18:40:26 +0200666 int
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000667generate_PUSHOBJ(cctx_T *cctx)
668{
669 RETURN_OK_IF_SKIP(cctx);
Ernie Rael5c018be2023-08-27 18:40:26 +0200670 if (generate_instr_type(cctx, ISN_PUSHOBJ, &t_object) == NULL)
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000671 return FAIL;
672 return OK;
673}
674
675/*
676 * Generate an ISN_PUSHCLASS instruction. "class" can be NULL.
677 */
678 static int
679generate_PUSHCLASS(cctx_T *cctx, class_T *class)
680{
681 RETURN_OK_IF_SKIP(cctx);
682 isn_T *isn = generate_instr_type(cctx, ISN_PUSHCLASS,
683 class == NULL ? &t_any : &class->class_type);
684 if (isn == NULL)
685 return FAIL;
Bram Moolenaar30a84472023-02-27 08:07:14 +0000686 isn->isn_arg.classarg = class;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000687 if (class != NULL)
688 ++class->class_refcount;
689 return OK;
690}
691
692/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000693 * Generate a PUSH instruction for "tv".
694 * "tv" will be consumed or cleared.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000695 */
696 int
697generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
698{
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100699 switch (tv->v_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000700 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100701 case VAR_BOOL:
702 generate_PUSHBOOL(cctx, tv->vval.v_number);
703 break;
704 case VAR_SPECIAL:
705 generate_PUSHSPEC(cctx, tv->vval.v_number);
706 break;
707 case VAR_NUMBER:
708 generate_PUSHNR(cctx, tv->vval.v_number);
709 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100710 case VAR_FLOAT:
711 generate_PUSHF(cctx, tv->vval.v_float);
712 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100713 case VAR_BLOB:
714 generate_PUSHBLOB(cctx, tv->vval.v_blob);
715 tv->vval.v_blob = NULL;
716 break;
717 case VAR_LIST:
718 if (tv->vval.v_list != NULL)
719 iemsg("non-empty list constant not supported");
720 generate_NEWLIST(cctx, 0, TRUE);
721 break;
722 case VAR_DICT:
723 if (tv->vval.v_dict != NULL)
724 iemsg("non-empty dict constant not supported");
725 generate_NEWDICT(cctx, 0, TRUE);
726 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000727#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100728 case VAR_JOB:
729 if (tv->vval.v_job != NULL)
730 iemsg("non-null job constant not supported");
731 generate_PUSHJOB(cctx);
732 break;
733 case VAR_CHANNEL:
734 if (tv->vval.v_channel != NULL)
735 iemsg("non-null channel constant not supported");
736 generate_PUSHCHANNEL(cctx);
737 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000738#endif
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100739 case VAR_FUNC:
740 if (tv->vval.v_string != NULL)
741 iemsg("non-null function constant not supported");
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100742 generate_PUSHFUNC(cctx, NULL, &t_func_unknown, TRUE);
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100743 break;
744 case VAR_PARTIAL:
745 if (tv->vval.v_partial != NULL)
746 iemsg("non-null partial constant not supported");
747 if (generate_instr_type(cctx, ISN_NEWPARTIAL, &t_func_unknown)
748 == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000749 return FAIL;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100750 break;
751 case VAR_STRING:
752 generate_PUSHS(cctx, &tv->vval.v_string);
753 tv->vval.v_string = NULL;
754 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000755 case VAR_OBJECT:
756 if (tv->vval.v_object != NULL)
757 {
758 emsg(_(e_cannot_use_non_null_object));
759 return FAIL;
760 }
761 generate_PUSHOBJ(cctx);
762 break;
763 case VAR_CLASS:
764 generate_PUSHCLASS(cctx, tv->vval.v_class);
765 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100766 default:
767 siemsg("constant type %d not supported", tv->v_type);
768 clear_tv(tv);
769 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000770 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100771 tv->v_type = VAR_UNKNOWN;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000772 return OK;
773}
774
775/*
776 * Generate an ISN_PUSHNR instruction.
777 */
778 int
779generate_PUSHNR(cctx_T *cctx, varnumber_T number)
780{
781 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000782
783 RETURN_OK_IF_SKIP(cctx);
784 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
785 return FAIL;
786 isn->isn_arg.number = number;
787
788 if (number == 0 || number == 1)
789 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000790 set_type_on_stack(cctx, &t_number_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000791 return OK;
792}
793
794/*
795 * Generate an ISN_PUSHBOOL instruction.
796 */
797 int
798generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
799{
800 isn_T *isn;
801
802 RETURN_OK_IF_SKIP(cctx);
803 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
804 return FAIL;
805 isn->isn_arg.number = number;
806
807 return OK;
808}
809
810/*
811 * Generate an ISN_PUSHSPEC instruction.
812 */
813 int
814generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
815{
816 isn_T *isn;
817
818 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000819 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC,
820 number == VVAL_NULL ? &t_null : &t_none)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000821 return FAIL;
822 isn->isn_arg.number = number;
823
824 return OK;
825}
826
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000827/*
828 * Generate an ISN_PUSHF instruction.
829 */
830 int
831generate_PUSHF(cctx_T *cctx, float_T fnumber)
832{
833 isn_T *isn;
834
835 RETURN_OK_IF_SKIP(cctx);
836 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
837 return FAIL;
838 isn->isn_arg.fnumber = fnumber;
839
840 return OK;
841}
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000842
843/*
844 * Generate an ISN_PUSHS instruction.
845 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100846 * Note that if "str" is used in the instruction OK is returned and "*str" is
847 * not set to NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000848 */
849 int
850generate_PUSHS(cctx_T *cctx, char_u **str)
851{
852 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100853 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000854
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100855 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000856 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100857 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
858 ret = FAIL;
859 else
860 {
861 isn->isn_arg.string = str == NULL ? NULL : *str;
862 return OK;
863 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000864 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100865 if (str != NULL)
866 VIM_CLEAR(*str);
867 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000868}
869
870/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000871 * Generate an ISN_PUSHCHANNEL instruction. Channel is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000872 */
873 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000874generate_PUSHCHANNEL(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000875{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000876 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100877#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100878 if (generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000879 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000880 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100881#else
882 emsg(_(e_channel_job_feature_not_available));
883 return FAIL;
884#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000885}
886
887/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000888 * Generate an ISN_PUSHJOB instruction. Job is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000889 */
890 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000891generate_PUSHJOB(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000892{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000893 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100894#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100895 if (generate_instr_type(cctx, ISN_PUSHJOB, &t_job) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000896 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000897 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100898#else
899 emsg(_(e_channel_job_feature_not_available));
900 return FAIL;
901#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000902}
903
904/*
905 * Generate an ISN_PUSHBLOB instruction.
906 * Consumes "blob".
907 */
908 int
909generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
910{
911 isn_T *isn;
912
913 RETURN_OK_IF_SKIP(cctx);
914 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
915 return FAIL;
916 isn->isn_arg.blob = blob;
917
918 return OK;
919}
920
921/*
922 * Generate an ISN_PUSHFUNC instruction with name "name".
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100923 * When "may_prefix" is TRUE prefix "g:" unless "name" is script-local or
924 * autoload.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000925 */
926 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100927generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type, int may_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000928{
929 isn_T *isn;
930 char_u *funcname;
931
932 RETURN_OK_IF_SKIP(cctx);
933 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
934 return FAIL;
935 if (name == NULL)
936 funcname = NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100937 else if (!may_prefix
938 || *name == K_SPECIAL // script-local
Bram Moolenaard041f422022-01-12 19:54:00 +0000939 || vim_strchr(name, AUTOLOAD_CHAR) != NULL) // autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000940 funcname = vim_strsave(name);
941 else
942 {
943 funcname = alloc(STRLEN(name) + 3);
944 if (funcname != NULL)
945 {
946 STRCPY(funcname, "g:");
947 STRCPY(funcname + 2, name);
948 }
949 }
950
951 isn->isn_arg.string = funcname;
952 return OK;
953}
954
955/*
Bram Moolenaar06b77222022-01-25 15:51:56 +0000956 * Generate an ISN_AUTOLOAD instruction.
957 */
958 int
959generate_AUTOLOAD(cctx_T *cctx, char_u *name, type_T *type)
960{
961 isn_T *isn;
962
963 RETURN_OK_IF_SKIP(cctx);
964 if ((isn = generate_instr_type(cctx, ISN_AUTOLOAD, type)) == NULL)
965 return FAIL;
966 isn->isn_arg.string = vim_strsave(name);
967 if (isn->isn_arg.string == NULL)
968 return FAIL;
969 return OK;
970}
971
972/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000973 * Generate an ISN_GETITEM instruction with "index".
974 * "with_op" is TRUE for "+=" and other operators, the stack has the current
975 * value below the list with values.
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100976 * Caller must check the type is a list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000977 */
978 int
979generate_GETITEM(cctx_T *cctx, int index, int with_op)
980{
981 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000982 type_T *type = get_type_on_stack(cctx, with_op ? 1 : 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000983 type_T *item_type = &t_any;
984
985 RETURN_OK_IF_SKIP(cctx);
986
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000987 item_type = type->tt_member;
988 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
989 return FAIL;
990 isn->isn_arg.getitem.gi_index = index;
991 isn->isn_arg.getitem.gi_with_op = with_op;
992
993 // add the item type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +0000994 return push_type_stack(cctx, item_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000995}
996
997/*
998 * Generate an ISN_SLICE instruction with "count".
999 */
1000 int
1001generate_SLICE(cctx_T *cctx, int count)
1002{
1003 isn_T *isn;
1004
1005 RETURN_OK_IF_SKIP(cctx);
1006 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1007 return FAIL;
1008 isn->isn_arg.number = count;
1009 return OK;
1010}
1011
1012/*
1013 * Generate an ISN_CHECKLEN instruction with "min_len".
1014 */
1015 int
1016generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1017{
1018 isn_T *isn;
1019
1020 RETURN_OK_IF_SKIP(cctx);
1021
1022 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1023 return FAIL;
1024 isn->isn_arg.checklen.cl_min_len = min_len;
1025 isn->isn_arg.checklen.cl_more_OK = more_OK;
1026
1027 return OK;
1028}
1029
1030/*
1031 * Generate an ISN_STORE instruction.
1032 */
1033 int
1034generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1035{
1036 isn_T *isn;
1037
1038 RETURN_OK_IF_SKIP(cctx);
1039 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1040 return FAIL;
1041 if (name != NULL)
1042 isn->isn_arg.string = vim_strsave(name);
1043 else
1044 isn->isn_arg.number = idx;
1045
1046 return OK;
1047}
1048
1049/*
Bram Moolenaard505d172022-12-18 21:42:55 +00001050 * Generate an ISN_LOAD_CLASSMEMBER ("load" == TRUE) or ISN_STORE_CLASSMEMBER
1051 * ("load" == FALSE) instruction.
1052 */
1053 int
1054generate_CLASSMEMBER(
1055 cctx_T *cctx,
1056 int load,
1057 class_T *cl,
1058 int idx)
1059{
1060 isn_T *isn;
1061
1062 RETURN_OK_IF_SKIP(cctx);
1063 if (load)
1064 {
1065 ocmember_T *m = &cl->class_class_members[idx];
1066 isn = generate_instr_type(cctx, ISN_LOAD_CLASSMEMBER, m->ocm_type);
1067 }
1068 else
1069 {
1070 isn = generate_instr_drop(cctx, ISN_STORE_CLASSMEMBER, 1);
1071 }
1072 if (isn == NULL)
1073 return FAIL;
1074 isn->isn_arg.classmember.cm_class = cl;
1075 ++cl->class_refcount;
1076 isn->isn_arg.classmember.cm_idx = idx;
1077
1078 return OK;
1079}
1080
1081/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001082 * Generate an ISN_STOREOUTER instruction.
1083 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +00001084 static int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001085generate_STOREOUTER(cctx_T *cctx, int idx, int level, int loop_idx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001086{
1087 isn_T *isn;
1088
1089 RETURN_OK_IF_SKIP(cctx);
1090 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1091 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001092 if (level == 1 && loop_idx >= 0 && idx >= loop_idx)
1093 {
1094 // Store a variable defined in a loop. A copy will be made at the end
1095 // of the loop. TODO: how about deeper nesting?
1096 isn->isn_arg.outer.outer_idx = idx - loop_idx;
1097 isn->isn_arg.outer.outer_depth = OUTER_LOOP_DEPTH;
1098 }
1099 else
1100 {
1101 isn->isn_arg.outer.outer_idx = idx;
1102 isn->isn_arg.outer.outer_depth = level;
1103 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001104
1105 return OK;
1106}
1107
1108/*
1109 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1110 */
1111 int
1112generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1113{
1114 isn_T *isn;
1115
1116 RETURN_OK_IF_SKIP(cctx);
1117 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1118 return FAIL;
1119 isn->isn_arg.storenr.stnr_idx = idx;
1120 isn->isn_arg.storenr.stnr_val = value;
1121
1122 return OK;
1123}
1124
1125/*
1126 * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction
1127 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +00001128 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001129generate_STOREOPT(
1130 cctx_T *cctx,
1131 isntype_T isn_type,
1132 char_u *name,
1133 int opt_flags)
1134{
1135 isn_T *isn;
1136
1137 RETURN_OK_IF_SKIP(cctx);
1138 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1139 return FAIL;
1140 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1141 isn->isn_arg.storeopt.so_flags = opt_flags;
1142
1143 return OK;
1144}
1145
1146/*
1147 * Generate an ISN_LOAD or similar instruction.
1148 */
1149 int
1150generate_LOAD(
1151 cctx_T *cctx,
1152 isntype_T isn_type,
1153 int idx,
1154 char_u *name,
1155 type_T *type)
1156{
1157 isn_T *isn;
1158
1159 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001160 if ((isn = generate_instr_type2(cctx, isn_type, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001161 return FAIL;
1162 if (name != NULL)
1163 isn->isn_arg.string = vim_strsave(name);
1164 else
1165 isn->isn_arg.number = idx;
1166
1167 return OK;
1168}
1169
1170/*
1171 * Generate an ISN_LOADOUTER instruction
1172 */
1173 int
1174generate_LOADOUTER(
1175 cctx_T *cctx,
1176 int idx,
1177 int nesting,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001178 int loop_depth,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001179 int loop_idx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001180 type_T *type)
1181{
1182 isn_T *isn;
1183
1184 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001185 if ((isn = generate_instr_type2(cctx, ISN_LOADOUTER, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001186 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001187 if (nesting == 1 && loop_idx >= 0 && idx >= loop_idx)
1188 {
1189 // Load a variable defined in a loop. A copy will be made at the end
Bram Moolenaarcc341812022-09-19 15:54:34 +01001190 // of the loop.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001191 isn->isn_arg.outer.outer_idx = idx - loop_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001192 isn->isn_arg.outer.outer_depth = -loop_depth - 1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001193 }
1194 else
1195 {
1196 isn->isn_arg.outer.outer_idx = idx;
1197 isn->isn_arg.outer.outer_depth = nesting;
1198 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001199
1200 return OK;
1201}
1202
1203/*
1204 * Generate an ISN_LOADV instruction for v:var.
1205 */
1206 int
1207generate_LOADV(
1208 cctx_T *cctx,
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001209 char_u *name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001210{
1211 int di_flags;
1212 int vidx = find_vim_var(name, &di_flags);
1213 type_T *type;
1214
1215 RETURN_OK_IF_SKIP(cctx);
1216 if (vidx < 0)
1217 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001218 semsg(_(e_variable_not_found_str), name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001219 return FAIL;
1220 }
Bram Moolenaard787e402021-12-24 21:36:12 +00001221 type = get_vim_var_type(vidx, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001222 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
1223}
1224
1225/*
1226 * Generate an ISN_UNLET instruction.
1227 */
1228 int
1229generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
1230{
1231 isn_T *isn;
1232
1233 RETURN_OK_IF_SKIP(cctx);
1234 if ((isn = generate_instr(cctx, isn_type)) == NULL)
1235 return FAIL;
1236 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1237 isn->isn_arg.unlet.ul_forceit = forceit;
1238
1239 return OK;
1240}
1241
1242/*
1243 * Generate an ISN_LOCKCONST instruction.
1244 */
1245 int
1246generate_LOCKCONST(cctx_T *cctx)
1247{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001248 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01001249 if (generate_instr(cctx, ISN_LOCKCONST) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001250 return FAIL;
1251 return OK;
1252}
1253
1254/*
1255 * Generate an ISN_LOADS instruction.
1256 */
1257 int
1258generate_OLDSCRIPT(
1259 cctx_T *cctx,
1260 isntype_T isn_type,
1261 char_u *name,
1262 int sid,
1263 type_T *type)
1264{
1265 isn_T *isn;
1266
1267 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001268 if (isn_type == ISN_LOADS || isn_type == ISN_LOADEXPORT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001269 isn = generate_instr_type(cctx, isn_type, type);
1270 else
1271 isn = generate_instr_drop(cctx, isn_type, 1);
1272 if (isn == NULL)
1273 return FAIL;
1274 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1275 isn->isn_arg.loadstore.ls_sid = sid;
1276
1277 return OK;
1278}
1279
1280/*
1281 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1282 */
1283 int
1284generate_VIM9SCRIPT(
1285 cctx_T *cctx,
1286 isntype_T isn_type,
1287 int sid,
1288 int idx,
1289 type_T *type)
1290{
1291 isn_T *isn;
1292 scriptref_T *sref;
1293 scriptitem_T *si = SCRIPT_ITEM(sid);
1294
1295 RETURN_OK_IF_SKIP(cctx);
1296 if (isn_type == ISN_LOADSCRIPT)
Bram Moolenaar160afdb2022-02-06 17:17:02 +00001297 isn = generate_instr_type2(cctx, isn_type, type, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001298 else
1299 isn = generate_instr_drop(cctx, isn_type, 1);
1300 if (isn == NULL)
1301 return FAIL;
1302
1303 // This requires three arguments, which doesn't fit in an instruction, thus
1304 // we need to allocate a struct for this.
1305 sref = ALLOC_ONE(scriptref_T);
1306 if (sref == NULL)
1307 return FAIL;
1308 isn->isn_arg.script.scriptref = sref;
1309 sref->sref_sid = sid;
1310 sref->sref_idx = idx;
1311 sref->sref_seq = si->sn_script_seq;
1312 sref->sref_type = type;
1313 return OK;
1314}
1315
1316/*
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001317 * Generate an ISN_NEWLIST instruction for "count" items.
1318 * "use_null" is TRUE for null_list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001319 */
1320 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001321generate_NEWLIST(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001322{
1323 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001324 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001325 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001326 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001327
1328 RETURN_OK_IF_SKIP(cctx);
1329 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1330 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001331 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001332
Bram Moolenaar078a4612022-01-04 15:17:03 +00001333 // Get the member type and the declared member type from all the items on
1334 // the stack.
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001335 member_type = get_member_type_from_stack(count, 1, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001336 type = get_list_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001337 decl_type = get_list_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001338
1339 // drop the value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001340 cctx->ctx_type_stack.ga_len -= count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001341
1342 // add the list type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001343 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001344}
1345
1346/*
1347 * Generate an ISN_NEWDICT instruction.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001348 * "use_null" is TRUE for null_dict.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001349 */
1350 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001351generate_NEWDICT(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001352{
1353 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001354 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001355 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001356 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001357
1358 RETURN_OK_IF_SKIP(cctx);
1359 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1360 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001361 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001362
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001363 member_type = get_member_type_from_stack(count, 2, cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001364 type = get_dict_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001365 decl_type = get_dict_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001366
1367 // drop the key and value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001368 cctx->ctx_type_stack.ga_len -= 2 * count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001369
1370 // add the dict type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001371 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001372}
1373
1374/*
1375 * Generate an ISN_FUNCREF instruction.
Bram Moolenaar313e4722023-02-08 20:55:27 +00001376 * For "obj.Method" "cl" is the class of the object (can be an interface or a
1377 * base class) and "fi" the index of the method on that class.
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001378 * "isn_idx" is set to the index of the instruction, so that fr_dfunc_idx can
1379 * be set later. The index is used instead of a pointer to the instruction
1380 * because the instruction memory can be reallocated.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001381 */
1382 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001383generate_FUNCREF(
1384 cctx_T *cctx,
1385 ufunc_T *ufunc,
Bram Moolenaar313e4722023-02-08 20:55:27 +00001386 class_T *cl,
1387 int fi,
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001388 int *isn_idx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001389{
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001390 isn_T *isn;
1391 type_T *type;
1392 funcref_extra_T *extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001393 loopvarinfo_T loopinfo;
1394 int has_vars;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001395
1396 RETURN_OK_IF_SKIP(cctx);
1397 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1398 return FAIL;
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001399 if (isn_idx != NULL)
1400 // save the index of the new instruction
1401 *isn_idx = cctx->ctx_instr.ga_len - 1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001402
Bram Moolenaarcc341812022-09-19 15:54:34 +01001403 has_vars = get_loop_var_info(cctx, &loopinfo);
Bram Moolenaar313e4722023-02-08 20:55:27 +00001404 if (ufunc->uf_def_status == UF_NOT_COMPILED || has_vars || cl != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001405 {
1406 extra = ALLOC_CLEAR_ONE(funcref_extra_T);
1407 if (extra == NULL)
1408 return FAIL;
1409 isn->isn_arg.funcref.fr_extra = extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001410 extra->fre_loopvar_info = loopinfo;
Bram Moolenaar313e4722023-02-08 20:55:27 +00001411 if (cl != NULL)
1412 {
1413 extra->fre_class = cl;
1414 ++cl->class_refcount;
1415 extra->fre_method_idx = fi;
1416 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001417 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00001418 if (ufunc->uf_def_status == UF_NOT_COMPILED || cl != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001419 extra->fre_func_name = vim_strsave(ufunc->uf_name);
Bram Moolenaar313e4722023-02-08 20:55:27 +00001420 if (ufunc->uf_def_status != UF_NOT_COMPILED && cl == NULL)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001421 {
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001422 if (isn_idx == NULL && ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001423 // compile the function now, we need the uf_dfunc_idx value
1424 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001425 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001426 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01001427
1428 // Reserve an extra variable to keep track of the number of closures
1429 // created.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001430 cctx->ctx_has_closure = 1;
1431
1432 // If the referenced function is a closure, it may use items further up in
Bram Moolenaar139575d2022-03-15 19:29:30 +00001433 // the nested context, including this one. But not a function defined at
1434 // the script level.
1435 if ((ufunc->uf_flags & FC_CLOSURE)
1436 && func_name_refcount(cctx->ctx_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001437 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1438
Bram Moolenaar078a4612022-01-04 15:17:03 +00001439 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1440 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001441}
1442
1443/*
1444 * Generate an ISN_NEWFUNC instruction.
1445 * "lambda_name" and "func_name" must be in allocated memory and will be
1446 * consumed.
1447 */
1448 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001449generate_NEWFUNC(
1450 cctx_T *cctx,
1451 char_u *lambda_name,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001452 char_u *func_name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001453{
1454 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001455 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001456
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001457 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001458 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001459 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1460 ret = FAIL;
1461 else
1462 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001463 newfuncarg_T *arg = ALLOC_CLEAR_ONE(newfuncarg_T);
1464
1465 if (arg == NULL)
1466 ret = FAIL;
1467 else
1468 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01001469 // Reserve an extra variable to keep track of the number of
1470 // closures created.
1471 cctx->ctx_has_closure = 1;
1472
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001473 isn->isn_arg.newfunc.nf_arg = arg;
1474 arg->nfa_lambda = lambda_name;
1475 arg->nfa_global = func_name;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001476 (void)get_loop_var_info(cctx, &arg->nfa_loopvar_info);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001477 return OK;
1478 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001479 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001480 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001481 vim_free(lambda_name);
1482 vim_free(func_name);
1483 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001484}
1485
1486/*
1487 * Generate an ISN_DEF instruction: list functions
1488 */
1489 int
1490generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1491{
1492 isn_T *isn;
1493
1494 RETURN_OK_IF_SKIP(cctx);
1495 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1496 return FAIL;
1497 if (len > 0)
1498 {
1499 isn->isn_arg.string = vim_strnsave(name, len);
1500 if (isn->isn_arg.string == NULL)
1501 return FAIL;
1502 }
1503 return OK;
1504}
1505
1506/*
1507 * Generate an ISN_JUMP instruction.
1508 */
1509 int
1510generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1511{
1512 isn_T *isn;
1513 garray_T *stack = &cctx->ctx_type_stack;
1514
1515 RETURN_OK_IF_SKIP(cctx);
1516 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1517 return FAIL;
1518 isn->isn_arg.jump.jump_when = when;
1519 isn->isn_arg.jump.jump_where = where;
1520
1521 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1522 --stack->ga_len;
1523
1524 return OK;
1525}
1526
1527/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001528 * Generate an ISN_WHILE instruction. Similar to ISN_JUMP for :while
1529 */
1530 int
1531generate_WHILE(cctx_T *cctx, int funcref_idx)
1532{
1533 isn_T *isn;
1534 garray_T *stack = &cctx->ctx_type_stack;
1535
1536 RETURN_OK_IF_SKIP(cctx);
1537 if ((isn = generate_instr(cctx, ISN_WHILE)) == NULL)
1538 return FAIL;
1539 isn->isn_arg.whileloop.while_funcref_idx = funcref_idx;
1540 isn->isn_arg.whileloop.while_end = 0; // filled in later
1541
1542 if (stack->ga_len > 0)
1543 --stack->ga_len;
1544
1545 return OK;
1546}
1547
1548/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001549 * Generate an ISN_JUMP_IF_ARG_SET or ISN_JUMP_IF_ARG_NOT_SET instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001550 */
1551 int
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001552generate_JUMP_IF_ARG(cctx_T *cctx, isntype_T isn_type, int arg_off)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001553{
1554 isn_T *isn;
1555
1556 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001557 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001558 return FAIL;
1559 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1560 // jump_where is set later
1561 return OK;
1562}
1563
1564 int
1565generate_FOR(cctx_T *cctx, int loop_idx)
1566{
1567 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001568
1569 RETURN_OK_IF_SKIP(cctx);
1570 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1571 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001572 isn->isn_arg.forloop.for_loop_idx = loop_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001573
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001574 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001575 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001576}
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001577
1578 int
Bram Moolenaarcc341812022-09-19 15:54:34 +01001579generate_ENDLOOP(cctx_T *cctx, loop_info_T *loop_info)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001580{
1581 isn_T *isn;
1582
1583 RETURN_OK_IF_SKIP(cctx);
1584 if ((isn = generate_instr(cctx, ISN_ENDLOOP)) == NULL)
1585 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001586 isn->isn_arg.endloop.end_depth = loop_info->li_depth;
1587 isn->isn_arg.endloop.end_funcref_idx = loop_info->li_funcref_idx;
1588 isn->isn_arg.endloop.end_var_idx = loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001589 isn->isn_arg.endloop.end_var_count =
Bram Moolenaarcc341812022-09-19 15:54:34 +01001590 cctx->ctx_locals.ga_len - loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001591 return OK;
1592}
1593
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001594/*
1595 * Generate an ISN_TRYCONT instruction.
1596 */
1597 int
1598generate_TRYCONT(cctx_T *cctx, int levels, int where)
1599{
1600 isn_T *isn;
1601
1602 RETURN_OK_IF_SKIP(cctx);
1603 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1604 return FAIL;
1605 isn->isn_arg.trycont.tct_levels = levels;
1606 isn->isn_arg.trycont.tct_where = where;
1607
1608 return OK;
1609}
1610
Bram Moolenaar16900322022-09-08 19:51:45 +01001611/*
1612 * Check "argount" arguments and their types on the type stack.
1613 * Give an error and return FAIL if something is wrong.
1614 * When "method_call" is NULL no code is generated.
1615 */
1616 int
1617check_internal_func_args(
1618 cctx_T *cctx,
1619 int func_idx,
1620 int argcount,
1621 int method_call,
1622 type2_T **argtypes,
1623 type2_T *shuffled_argtypes)
1624{
1625 garray_T *stack = &cctx->ctx_type_stack;
1626 int argoff = check_internal_func(func_idx, argcount);
1627
1628 if (argoff < 0)
1629 return FAIL;
1630
1631 if (method_call && argoff > 1)
1632 {
Bram Moolenaarb7f22702023-04-27 16:24:07 +01001633 if (argcount < argoff)
1634 {
1635 semsg(_(e_not_enough_arguments_for_function_str),
1636 internal_func_name(func_idx));
1637 return FAIL;
1638 }
Bram Moolenaar16900322022-09-08 19:51:45 +01001639
Bram Moolenaarb7f22702023-04-27 16:24:07 +01001640 isn_T *isn = generate_instr(cctx, ISN_SHUFFLE);
Bram Moolenaar16900322022-09-08 19:51:45 +01001641 if (isn == NULL)
1642 return FAIL;
1643 isn->isn_arg.shuffle.shfl_item = argcount;
1644 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1645 }
1646
1647 if (argcount > 0)
1648 {
1649 type2_T *typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
1650
1651 // Check the types of the arguments.
1652 if (method_call && argoff > 1)
1653 {
1654 int i;
1655
1656 for (i = 0; i < argcount; ++i)
1657 shuffled_argtypes[i] = (i < argoff - 1)
1658 ? typep[i + 1]
1659 : (i == argoff - 1) ? typep[0] : typep[i];
1660 *argtypes = shuffled_argtypes;
1661 }
1662 else
1663 {
1664 int i;
1665
1666 for (i = 0; i < argcount; ++i)
1667 shuffled_argtypes[i] = typep[i];
1668 *argtypes = shuffled_argtypes;
1669 }
1670 if (internal_func_check_arg_types(*argtypes, func_idx, argcount,
1671 cctx) == FAIL)
1672 return FAIL;
1673 }
1674 return OK;
1675}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001676
1677/*
1678 * Generate an ISN_BCALL instruction.
1679 * "method_call" is TRUE for "value->method()"
1680 * Return FAIL if the number of arguments is wrong.
1681 */
1682 int
1683generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1684{
1685 isn_T *isn;
1686 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001687 type2_T *argtypes = NULL;
1688 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1689 type2_T *maptype = NULL;
1690 type_T *type;
Bram Moolenaar81330182022-02-01 12:11:58 +00001691 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001692
1693 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar16900322022-09-08 19:51:45 +01001694
1695 if (check_internal_func_args(cctx, func_idx, argcount, method_call,
1696 &argtypes, shuffled_argtypes) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001697 return FAIL;
1698
Bram Moolenaar16900322022-09-08 19:51:45 +01001699 if (internal_func_is_map(func_idx))
1700 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001701
1702 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1703 return FAIL;
1704 isn->isn_arg.bfunc.cbf_idx = func_idx;
1705 isn->isn_arg.bfunc.cbf_argcount = argcount;
1706
1707 // Drop the argument types and push the return type.
1708 stack->ga_len -= argcount;
Bram Moolenaar32517c42023-01-15 18:17:12 +00001709 type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type,
1710 cctx->ctx_type_list);
Bram Moolenaar81330182022-02-01 12:11:58 +00001711 if (push_type_stack2(cctx, type, decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001712 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001713
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001714 if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
1715 && maptype[0].type_decl->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001716 // Check that map() didn't change the item types.
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001717 generate_TYPECHECK(cctx, maptype[0].type_decl, FALSE, -1, FALSE, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001718
1719 return OK;
1720}
1721
1722/*
1723 * Generate an ISN_LISTAPPEND instruction. Works like add().
1724 * Argument count is already checked.
1725 */
1726 int
1727generate_LISTAPPEND(cctx_T *cctx)
1728{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001729 type_T *list_type;
1730 type_T *item_type;
1731 type_T *expected;
1732
1733 // Caller already checked that list_type is a list.
Bram Moolenaar81330182022-02-01 12:11:58 +00001734 // For checking the item type we use the declared type of the list and the
1735 // current type of the added item, adding a string to [1, 2] is OK.
1736 list_type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaarfa103972022-09-29 19:14:42 +01001737 if (arg_type_modifiable(list_type, 1) == FAIL)
1738 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001739 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001740 expected = list_type->tt_member;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001741 if (need_type(item_type, expected, FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001742 return FAIL;
1743
1744 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1745 return FAIL;
1746
Bram Moolenaar078a4612022-01-04 15:17:03 +00001747 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001748 return OK;
1749}
1750
1751/*
1752 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1753 * Argument count is already checked.
1754 */
1755 int
1756generate_BLOBAPPEND(cctx_T *cctx)
1757{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001758 type_T *item_type;
1759
Bram Moolenaarfa103972022-09-29 19:14:42 +01001760 // Caller already checked that blob_type is a blob, check it is modifiable.
1761 if (arg_type_modifiable(get_decl_type_on_stack(cctx, 1), 1) == FAIL)
1762 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001763 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001764 if (need_type(item_type, &t_number, FALSE,
1765 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001766 return FAIL;
1767
1768 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1769 return FAIL;
1770
Bram Moolenaar078a4612022-01-04 15:17:03 +00001771 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001772 return OK;
1773}
1774
1775/*
Bram Moolenaard0200c82023-01-28 15:19:40 +00001776 * Generate an ISN_DCALL, ISN_UCALL or ISN_METHODCALL instruction.
1777 * When calling a method on an object, of which we know the interface only,
1778 * then "cl" is the interface and "mi" the method index on the interface.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001779 * Return FAIL if the number of arguments is wrong.
1780 */
1781 int
Bram Moolenaard0200c82023-01-28 15:19:40 +00001782generate_CALL(
1783 cctx_T *cctx,
1784 ufunc_T *ufunc,
1785 class_T *cl,
1786 int mi,
1787 int pushed_argcount)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001788{
1789 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001790 int regular_args = ufunc->uf_args.ga_len;
1791 int argcount = pushed_argcount;
1792
1793 RETURN_OK_IF_SKIP(cctx);
1794 if (argcount > regular_args && !has_varargs(ufunc))
1795 {
1796 semsg(_(e_too_many_arguments_for_function_str),
1797 printable_func_name(ufunc));
1798 return FAIL;
1799 }
1800 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1801 {
1802 semsg(_(e_not_enough_arguments_for_function_str),
1803 printable_func_name(ufunc));
1804 return FAIL;
1805 }
1806
1807 if (ufunc->uf_def_status != UF_NOT_COMPILED
1808 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1809 {
1810 int i;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001811 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001812
1813 for (i = 0; i < argcount; ++i)
1814 {
1815 type_T *expected;
1816 type_T *actual;
1817
Bram Moolenaar078a4612022-01-04 15:17:03 +00001818 actual = get_type_on_stack(cctx, argcount - i - 1);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001819 if (actual->tt_type == VAR_SPECIAL
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001820 && i >= regular_args - ufunc->uf_def_args.ga_len)
1821 {
1822 // assume v:none used for default argument value
1823 continue;
1824 }
1825 if (i < regular_args)
1826 {
1827 if (ufunc->uf_arg_types == NULL)
1828 continue;
1829 expected = ufunc->uf_arg_types[i];
1830 }
1831 else if (ufunc->uf_va_type == NULL
1832 || ufunc->uf_va_type == &t_list_any)
1833 // possibly a lambda or "...: any"
1834 expected = &t_any;
1835 else
1836 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001837 if (need_type(actual, expected, FALSE,
1838 -argcount + i, i + 1, cctx, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001839 {
1840 arg_type_mismatch(expected, actual, i + 1);
1841 return FAIL;
1842 }
1843 }
Bram Moolenaar139575d2022-03-15 19:29:30 +00001844 compile_type = get_compile_type(ufunc);
1845 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001846 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaar139575d2022-03-15 19:29:30 +00001847 compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001848 return FAIL;
1849 }
1850 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1851 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001852 emsg_funcname(e_call_to_function_that_failed_to_compile_str,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001853 ufunc->uf_name);
1854 return FAIL;
1855 }
1856
Bram Moolenaard0200c82023-01-28 15:19:40 +00001857 if ((isn = generate_instr(cctx, cl != NULL ? ISN_METHODCALL
1858 : ufunc->uf_def_status != UF_NOT_COMPILED
1859 ? ISN_DCALL : ISN_UCALL)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001860 return FAIL;
Bram Moolenaar836137d2023-01-29 14:11:24 +00001861 if (cl != NULL /* isn->isn_type == ISN_METHODCALL */)
Bram Moolenaard0200c82023-01-28 15:19:40 +00001862 {
1863 isn->isn_arg.mfunc = ALLOC_ONE(cmfunc_T);
1864 if (isn->isn_arg.mfunc == NULL)
1865 return FAIL;
1866 isn->isn_arg.mfunc->cmf_itf = cl;
1867 ++cl->class_refcount;
1868 isn->isn_arg.mfunc->cmf_idx = mi;
1869 isn->isn_arg.mfunc->cmf_argcount = argcount;
1870 }
1871 else if (isn->isn_type == ISN_DCALL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001872 {
1873 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1874 isn->isn_arg.dfunc.cdf_argcount = argcount;
1875 }
1876 else
1877 {
1878 // A user function may be deleted and redefined later, can't use the
1879 // ufunc pointer, need to look it up again at runtime.
1880 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1881 isn->isn_arg.ufunc.cuf_argcount = argcount;
1882 }
1883
Bram Moolenaar078a4612022-01-04 15:17:03 +00001884 // drop the argument types
1885 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001886
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02001887 // For an object or class method call, drop the object/class type.
Yegappan Lakshmanan639751d2023-08-27 19:23:37 +02001888 if (ufunc->uf_class != NULL)
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02001889 {
1890 // When a class method is called without the class name prefix, then
1891 // the type will not be in the stack.
1892 type_T *stype = get_type_on_stack(cctx, 0);
1893 if (stype->tt_type == VAR_CLASS || stype->tt_type == VAR_OBJECT)
1894 cctx->ctx_type_stack.ga_len--;
1895 }
Yegappan Lakshmanan639751d2023-08-27 19:23:37 +02001896
Bram Moolenaar078a4612022-01-04 15:17:03 +00001897 // add return type
1898 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001899}
1900
1901/*
1902 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1903 */
1904 int
1905generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1906{
1907 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001908
1909 RETURN_OK_IF_SKIP(cctx);
1910 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1911 return FAIL;
1912 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1913 isn->isn_arg.ufunc.cuf_argcount = argcount;
1914
Bram Moolenaar078a4612022-01-04 15:17:03 +00001915 // drop the argument types
1916 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001917
Bram Moolenaar078a4612022-01-04 15:17:03 +00001918 // add return value
1919 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001920}
1921
1922/*
Bram Moolenaar16900322022-09-08 19:51:45 +01001923 * Check the arguments of function "type" against the types on the stack.
1924 * Returns OK or FAIL;
1925 */
1926 int
1927check_func_args_from_type(
1928 cctx_T *cctx,
1929 type_T *type,
1930 int argcount,
1931 int at_top,
1932 char_u *name)
1933{
1934 if (type->tt_argcount != -1)
1935 {
1936 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1937
1938 if (argcount < type->tt_min_argcount - varargs)
1939 {
1940 emsg_funcname(e_not_enough_arguments_for_function_str, name);
1941 return FAIL;
1942 }
1943 if (!varargs && argcount > type->tt_argcount)
1944 {
1945 emsg_funcname(e_too_many_arguments_for_function_str, name);
1946 return FAIL;
1947 }
1948 if (type->tt_args != NULL)
1949 {
1950 int i;
1951
1952 for (i = 0; i < argcount; ++i)
1953 {
1954 int offset = -argcount + i - (at_top ? 0 : 1);
1955 type_T *actual = get_type_on_stack(cctx, -1 - offset);
1956 type_T *expected;
1957
1958 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaar36818a92023-01-03 12:33:26 +00001959 {
1960 expected = type->tt_args[type->tt_argcount - 1];
1961 if (expected != NULL && expected->tt_type == VAR_LIST)
1962 expected = expected->tt_member;
1963 if (expected == NULL)
1964 expected = &t_any;
1965 }
Bram Moolenaar16900322022-09-08 19:51:45 +01001966 else if (i >= type->tt_min_argcount
1967 && actual->tt_type == VAR_SPECIAL)
1968 expected = &t_any;
1969 else
1970 expected = type->tt_args[i];
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001971 if (need_type(actual, expected, FALSE,
1972 offset, i + 1, cctx, TRUE, FALSE) == FAIL)
Bram Moolenaar16900322022-09-08 19:51:45 +01001973 {
1974 arg_type_mismatch(expected, actual, i + 1);
1975 return FAIL;
1976 }
1977 }
1978 }
1979 }
1980
1981 return OK;
1982}
1983/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001984 * Generate an ISN_PCALL instruction.
1985 * "type" is the type of the FuncRef.
1986 */
1987 int
1988generate_PCALL(
1989 cctx_T *cctx,
1990 int argcount,
1991 char_u *name,
1992 type_T *type,
1993 int at_top)
1994{
1995 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001996 type_T *ret_type;
1997
1998 RETURN_OK_IF_SKIP(cctx);
1999
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002000 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002001 ret_type = &t_any;
2002 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
2003 {
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002004 if (check_func_args_from_type(cctx, type, argcount, at_top, name)
2005 == FAIL)
Bram Moolenaar16900322022-09-08 19:51:45 +01002006 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002007
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002008 ret_type = type->tt_member;
2009 if (ret_type == &t_unknown)
2010 // return type not known yet, use a runtime check
2011 ret_type = &t_any;
2012 }
2013 else
2014 {
2015 semsg(_(e_not_callable_type_str), name);
2016 return FAIL;
2017 }
2018
2019 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2020 return FAIL;
2021 isn->isn_arg.pfunc.cpf_top = at_top;
2022 isn->isn_arg.pfunc.cpf_argcount = argcount;
2023
Bram Moolenaar078a4612022-01-04 15:17:03 +00002024 // drop the arguments and the funcref/partial
2025 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002026
Bram Moolenaar078a4612022-01-04 15:17:03 +00002027 // push the return value
2028 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002029
2030 // If partial is above the arguments it must be cleared and replaced with
2031 // the return value.
2032 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2033 return FAIL;
2034
2035 return OK;
2036}
2037
2038/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002039 * Generate an ISN_DEFER instruction.
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00002040 * "obj_method" is one for "obj.Method()", zero otherwise.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002041 */
2042 int
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00002043generate_DEFER(cctx_T *cctx, int var_idx, int obj_method, int argcount)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002044{
2045 isn_T *isn;
2046
2047 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00002048 if ((isn = generate_instr_drop(cctx,
2049 obj_method == 0 ? ISN_DEFER : ISN_DEFEROBJ,
2050 argcount + 1)) == NULL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002051 return FAIL;
2052 isn->isn_arg.defer.defer_var_idx = var_idx;
2053 isn->isn_arg.defer.defer_argcount = argcount;
2054 return OK;
2055}
2056
2057/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002058 * Generate an ISN_STRINGMEMBER instruction.
2059 */
2060 int
2061generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
2062{
2063 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002064 type_T *type;
2065
2066 RETURN_OK_IF_SKIP(cctx);
2067 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
2068 return FAIL;
2069 isn->isn_arg.string = vim_strnsave(name, len);
2070
2071 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00002072 type = get_type_on_stack(cctx, 0);
Bram Moolenaar0089ce22022-10-08 14:39:36 +01002073 if (type->tt_type != VAR_DICT
2074 && type->tt_type != VAR_ANY && type->tt_type != VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002075 {
2076 char *tofree;
2077
2078 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
2079 name, type_name(type, &tofree));
2080 vim_free(tofree);
2081 return FAIL;
2082 }
2083 // change dict type to dict member type
2084 if (type->tt_type == VAR_DICT)
2085 {
Bram Moolenaar0089ce22022-10-08 14:39:36 +01002086 type_T *ntype = type->tt_member->tt_type == VAR_UNKNOWN
Bram Moolenaar078a4612022-01-04 15:17:03 +00002087 ? &t_any : type->tt_member;
2088 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002089 }
2090
2091 return OK;
2092}
2093
2094/*
2095 * Generate an ISN_ECHO instruction.
2096 */
2097 int
2098generate_ECHO(cctx_T *cctx, int with_white, int count)
2099{
2100 isn_T *isn;
2101
2102 RETURN_OK_IF_SKIP(cctx);
2103 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2104 return FAIL;
2105 isn->isn_arg.echo.echo_with_white = with_white;
2106 isn->isn_arg.echo.echo_count = count;
2107
2108 return OK;
2109}
2110
2111/*
2112 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
2113 */
2114 int
2115generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
2116{
2117 isn_T *isn;
2118
Bram Moolenaar2eae3d22022-10-07 15:09:27 +01002119 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002120 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
2121 return FAIL;
2122 isn->isn_arg.number = count;
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002123 return OK;
2124}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002125
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002126/*
2127 * Generate an ISN_ECHOWINDOW instruction
2128 */
2129 int
2130generate_ECHOWINDOW(cctx_T *cctx, int count, long time)
2131{
2132 isn_T *isn;
2133
2134 if ((isn = generate_instr_drop(cctx, ISN_ECHOWINDOW, count)) == NULL)
2135 return FAIL;
2136 isn->isn_arg.echowin.ewin_count = count;
2137 isn->isn_arg.echowin.ewin_time = time;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002138 return OK;
2139}
2140
2141/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002142 * Generate an ISN_SOURCE instruction.
2143 */
2144 int
2145generate_SOURCE(cctx_T *cctx, int sid)
2146{
2147 isn_T *isn;
2148
2149 if ((isn = generate_instr(cctx, ISN_SOURCE)) == NULL)
2150 return FAIL;
2151 isn->isn_arg.number = sid;
2152
2153 return OK;
2154}
2155
2156/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002157 * Generate an ISN_PUT instruction.
2158 */
2159 int
2160generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2161{
2162 isn_T *isn;
2163
2164 RETURN_OK_IF_SKIP(cctx);
2165 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2166 return FAIL;
2167 isn->isn_arg.put.put_regname = regname;
2168 isn->isn_arg.put.put_lnum = lnum;
2169 return OK;
2170}
2171
2172/*
2173 * Generate an EXEC instruction that takes a string argument.
2174 * A copy is made of "line".
2175 */
2176 int
Ernie Raelee865f32023-09-29 19:53:55 +02002177generate_LOCKUNLOCK(cctx_T *cctx, char_u *line, int is_arg)
2178{
2179 isn_T *isn;
2180
2181 RETURN_OK_IF_SKIP(cctx);
2182 if ((isn = generate_instr(cctx, ISN_LOCKUNLOCK)) == NULL)
2183 return FAIL;
2184 isn->isn_arg.lockunlock.string = vim_strsave(line);
2185 isn->isn_arg.lockunlock.is_arg = is_arg;
2186 return OK;
2187}
2188
2189/*
2190 * Generate an EXEC instruction that takes a string argument.
2191 * A copy is made of "line".
2192 */
2193 int
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002194generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
2195{
2196 isn_T *isn;
2197
2198 RETURN_OK_IF_SKIP(cctx);
2199 if ((isn = generate_instr(cctx, isntype)) == NULL)
2200 return FAIL;
2201 isn->isn_arg.string = vim_strsave(line);
2202 return OK;
2203}
2204
2205/*
2206 * Generate an EXEC instruction that takes a string argument.
2207 * "str" must be allocated, it is consumed.
2208 */
2209 int
2210generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
2211{
2212 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002213 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002214
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002215 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002216 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002217 if ((isn = generate_instr(cctx, isntype)) == NULL)
2218 ret = FAIL;
2219 else
2220 {
2221 isn->isn_arg.string = str;
2222 return OK;
2223 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002224 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002225 vim_free(str);
2226 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002227}
2228
2229 int
2230generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2231{
2232 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002233
2234 RETURN_OK_IF_SKIP(cctx);
2235 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2236 return FAIL;
2237 isn->isn_arg.string = vim_strsave(line);
2238
Bram Moolenaar078a4612022-01-04 15:17:03 +00002239 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002240}
2241
2242 int
2243generate_EXECCONCAT(cctx_T *cctx, int count)
2244{
2245 isn_T *isn;
2246
2247 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2248 return FAIL;
2249 isn->isn_arg.number = count;
2250 return OK;
2251}
2252
2253/*
2254 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2255 */
2256 int
2257generate_RANGE(cctx_T *cctx, char_u *range)
2258{
2259 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002260
2261 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2262 return FAIL;
2263 isn->isn_arg.string = range;
2264
Bram Moolenaar078a4612022-01-04 15:17:03 +00002265 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002266}
2267
2268 int
2269generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2270{
2271 isn_T *isn;
2272
2273 RETURN_OK_IF_SKIP(cctx);
2274 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2275 return FAIL;
2276 isn->isn_arg.unpack.unp_count = var_count;
2277 isn->isn_arg.unpack.unp_semicolon = semicolon;
2278 return OK;
2279}
2280
2281/*
2282 * Generate an instruction for any command modifiers.
2283 */
2284 int
2285generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
2286{
2287 isn_T *isn;
2288
2289 if (has_cmdmod(cmod, FALSE))
2290 {
2291 cctx->ctx_has_cmdmod = TRUE;
2292
2293 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
2294 return FAIL;
2295 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2296 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2297 return FAIL;
2298 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
2299 // filter program now belongs to the instruction
2300 cmod->cmod_filter_regmatch.regprog = NULL;
2301 }
2302
2303 return OK;
2304}
2305
2306 int
2307generate_undo_cmdmods(cctx_T *cctx)
2308{
2309 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2310 return FAIL;
2311 cctx->ctx_has_cmdmod = FALSE;
2312 return OK;
2313}
2314
2315/*
2316 * Generate a STORE instruction for "dest", not being "dest_local".
Bram Moolenaard505d172022-12-18 21:42:55 +00002317 * "lhs" might be NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002318 * Return FAIL when out of memory.
2319 */
2320 int
2321generate_store_var(
2322 cctx_T *cctx,
2323 assign_dest_T dest,
2324 int opt_flags,
2325 int vimvaridx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002326 type_T *type,
Bram Moolenaard505d172022-12-18 21:42:55 +00002327 char_u *name,
2328 lhs_T *lhs)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002329{
2330 switch (dest)
2331 {
2332 case dest_option:
2333 return generate_STOREOPT(cctx, ISN_STOREOPT,
2334 skip_option_env_lead(name), opt_flags);
2335 case dest_func_option:
2336 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
2337 skip_option_env_lead(name), opt_flags);
2338 case dest_global:
2339 // include g: with the name, easier to execute that way
2340 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
2341 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
2342 case dest_buffer:
2343 // include b: with the name, easier to execute that way
2344 return generate_STORE(cctx, ISN_STOREB, 0, name);
2345 case dest_window:
2346 // include w: with the name, easier to execute that way
2347 return generate_STORE(cctx, ISN_STOREW, 0, name);
2348 case dest_tab:
2349 // include t: with the name, easier to execute that way
2350 return generate_STORE(cctx, ISN_STORET, 0, name);
2351 case dest_env:
2352 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
2353 case dest_reg:
2354 return generate_STORE(cctx, ISN_STOREREG,
2355 name[1] == '@' ? '"' : name[1], NULL);
2356 case dest_vimvar:
2357 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
2358 case dest_script:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002359 {
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002360 int scriptvar_idx = lhs->lhs_scriptvar_idx;
2361 int scriptvar_sid = lhs->lhs_scriptvar_sid;
2362 if (scriptvar_idx < 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002363 {
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002364 isntype_T isn_type = ISN_STORES;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002365
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002366 if (SCRIPT_ID_VALID(scriptvar_sid)
2367 && SCRIPT_ITEM(scriptvar_sid)->sn_import_autoload
2368 && SCRIPT_ITEM(scriptvar_sid)->sn_autoload_prefix
2369 == NULL)
2370 {
2371 // "import autoload './dir/script.vim'" - load script
2372 // first
2373 if (generate_SOURCE(cctx, scriptvar_sid) == FAIL)
2374 return FAIL;
2375 isn_type = ISN_STOREEXPORT;
2376 }
2377
2378 // "s:" may be included in the name.
2379 return generate_OLDSCRIPT(cctx, isn_type, name,
2380 scriptvar_sid, type);
2381 }
2382 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002383 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002384 }
Bram Moolenaard505d172022-12-18 21:42:55 +00002385 case dest_class_member:
2386 return generate_CLASSMEMBER(cctx, FALSE,
2387 lhs->lhs_class, lhs->lhs_classmember_idx);
2388
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002389 case dest_local:
2390 case dest_expr:
2391 // cannot happen
2392 break;
2393 }
2394 return FAIL;
2395}
2396
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002397/*
2398 * Return TRUE when inside a "for" or "while" loop.
2399 */
2400 int
2401inside_loop_scope(cctx_T *cctx)
2402{
2403 scope_T *scope = cctx->ctx_scope;
2404
2405 for (;;)
2406 {
2407 if (scope == NULL)
2408 break;
2409 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
2410 return TRUE;
2411 scope = scope->se_outer;
2412 }
2413 return FALSE;
2414}
2415
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002416 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002417generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002418{
2419 if (lhs->lhs_dest != dest_local)
2420 return generate_store_var(cctx, lhs->lhs_dest,
2421 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
Bram Moolenaard505d172022-12-18 21:42:55 +00002422 lhs->lhs_type, lhs->lhs_name, lhs);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002423
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002424 if (lhs->lhs_lvar == NULL)
2425 return OK;
2426
2427 garray_T *instr = &cctx->ctx_instr;
2428 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2429
2430 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
2431 // ISN_STORENR.
2432 // And "var = 0" does not need any instruction.
2433 if (lhs->lhs_lvar->lv_from_outer == 0
2434 && instr->ga_len == instr_count + 1
2435 && isn->isn_type == ISN_PUSHNR)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002436 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002437 varnumber_T val = isn->isn_arg.number;
2438 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002439
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002440 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002441 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002442 // zero is the default value, no need to do anything
2443 --instr->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002444 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002445 else
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002446 {
2447 isn->isn_type = ISN_STORENR;
2448 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
2449 isn->isn_arg.storenr.stnr_val = val;
2450 }
2451 if (stack->ga_len > 0)
2452 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002453 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002454 else if (lhs->lhs_lvar->lv_from_outer > 0)
2455 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
2456 lhs->lhs_lvar->lv_from_outer, lhs->lhs_lvar->lv_loop_idx);
2457 else
2458 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002459 return OK;
2460}
2461
2462#if defined(FEAT_PROFILE) || defined(PROTO)
2463 void
2464may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2465{
2466 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
2467 generate_instr(cctx, ISN_PROF_END);
2468}
2469#endif
2470
2471
2472/*
2473 * Delete an instruction, free what it contains.
2474 */
2475 void
2476delete_instr(isn_T *isn)
2477{
2478 switch (isn->isn_type)
2479 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002480 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002481 case ISN_DEF:
2482 case ISN_EXEC:
2483 case ISN_EXECRANGE:
2484 case ISN_EXEC_SPLIT:
2485 case ISN_LEGACY_EVAL:
2486 case ISN_LOADAUTO:
2487 case ISN_LOADB:
2488 case ISN_LOADENV:
2489 case ISN_LOADG:
2490 case ISN_LOADOPT:
2491 case ISN_LOADT:
2492 case ISN_LOADW:
2493 case ISN_LOCKUNLOCK:
2494 case ISN_PUSHEXC:
2495 case ISN_PUSHFUNC:
2496 case ISN_PUSHS:
2497 case ISN_RANGE:
2498 case ISN_STOREAUTO:
2499 case ISN_STOREB:
2500 case ISN_STOREENV:
2501 case ISN_STOREG:
2502 case ISN_STORET:
2503 case ISN_STOREW:
2504 case ISN_STRINGMEMBER:
2505 vim_free(isn->isn_arg.string);
2506 break;
2507
2508 case ISN_SUBSTITUTE:
2509 {
2510 int idx;
2511 isn_T *list = isn->isn_arg.subs.subs_instr;
2512
2513 vim_free(isn->isn_arg.subs.subs_cmd);
2514 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2515 delete_instr(list + idx);
2516 vim_free(list);
2517 }
2518 break;
2519
2520 case ISN_INSTR:
2521 {
2522 int idx;
2523 isn_T *list = isn->isn_arg.instr;
2524
2525 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2526 delete_instr(list + idx);
2527 vim_free(list);
2528 }
2529 break;
2530
2531 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002532 case ISN_LOADEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002533 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002534 case ISN_STOREEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002535 vim_free(isn->isn_arg.loadstore.ls_name);
2536 break;
2537
2538 case ISN_UNLET:
2539 case ISN_UNLETENV:
2540 vim_free(isn->isn_arg.unlet.ul_name);
2541 break;
2542
2543 case ISN_STOREOPT:
2544 case ISN_STOREFUNCOPT:
2545 vim_free(isn->isn_arg.storeopt.so_name);
2546 break;
2547
2548 case ISN_PUSHBLOB: // push blob isn_arg.blob
2549 blob_unref(isn->isn_arg.blob);
2550 break;
2551
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002552 case ISN_PUSHCLASS:
Bram Moolenaar30a84472023-02-27 08:07:14 +00002553 class_unref(isn->isn_arg.classarg);
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002554 break;
2555
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002556 case ISN_UCALL:
2557 vim_free(isn->isn_arg.ufunc.cuf_name);
2558 break;
2559
2560 case ISN_FUNCREF:
2561 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002562 funcref_T *funcref = &isn->isn_arg.funcref;
2563 funcref_extra_T *extra = funcref->fr_extra;
2564
2565 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002566 {
2567 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002568 + funcref->fr_dfunc_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002569 ufunc_T *ufunc = dfunc->df_ufunc;
2570
2571 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2572 func_ptr_unref(ufunc);
2573 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002574 if (extra != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002575 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002576 char_u *name = extra->fre_func_name;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002577
2578 if (name != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002579 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002580 func_unref(name);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002581 vim_free(name);
2582 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00002583 if (extra->fre_class != NULL)
2584 class_unref(extra->fre_class);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002585 vim_free(extra);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002586 }
2587 }
2588 break;
2589
2590 case ISN_DCALL:
2591 {
2592 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002593 + isn->isn_arg.dfunc.cdf_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002594
2595 if (dfunc->df_ufunc != NULL
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002596 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002597 func_ptr_unref(dfunc->df_ufunc);
2598 }
2599 break;
2600
Bram Moolenaard0200c82023-01-28 15:19:40 +00002601 case ISN_METHODCALL:
2602 {
2603 cmfunc_T *mfunc = isn->isn_arg.mfunc;
2604 class_unref(mfunc->cmf_itf);
2605 vim_free(mfunc);
2606 }
2607 break;
2608
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002609 case ISN_NEWFUNC:
2610 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002611 newfuncarg_T *arg = isn->isn_arg.newfunc.nf_arg;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002612
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002613 if (arg != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002614 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002615 ufunc_T *ufunc = find_func_even_dead(
2616 arg->nfa_lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002617
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002618 if (ufunc != NULL)
2619 {
2620 unlink_def_function(ufunc);
2621 func_ptr_unref(ufunc);
2622 }
2623
2624 vim_free(arg->nfa_lambda);
2625 vim_free(arg->nfa_global);
2626 vim_free(arg);
2627 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002628 }
2629 break;
2630
2631 case ISN_CHECKTYPE:
2632 case ISN_SETTYPE:
2633 free_type(isn->isn_arg.type.ct_type);
2634 break;
2635
2636 case ISN_CMDMOD:
2637 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002638 ->cmod_filter_regmatch.regprog);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002639 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2640 break;
2641
2642 case ISN_LOADSCRIPT:
2643 case ISN_STORESCRIPT:
2644 vim_free(isn->isn_arg.script.scriptref);
2645 break;
2646
Bram Moolenaard505d172022-12-18 21:42:55 +00002647 case ISN_LOAD_CLASSMEMBER:
2648 case ISN_STORE_CLASSMEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00002649 case ISN_GET_ITF_MEMBER:
Bram Moolenaard505d172022-12-18 21:42:55 +00002650 class_unref(isn->isn_arg.classmember.cm_class);
2651 break;
2652
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002653 case ISN_STOREINDEX:
2654 class_unref(isn->isn_arg.storeindex.si_class);
2655 break;
2656
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002657 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002658 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002659 break;
2660
2661 case ISN_CEXPR_CORE:
2662 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2663 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2664 break;
2665
2666 case ISN_2BOOL:
2667 case ISN_2STRING:
2668 case ISN_2STRING_ANY:
2669 case ISN_ADDBLOB:
2670 case ISN_ADDLIST:
2671 case ISN_ANYINDEX:
2672 case ISN_ANYSLICE:
2673 case ISN_BCALL:
2674 case ISN_BLOBAPPEND:
2675 case ISN_BLOBINDEX:
2676 case ISN_BLOBSLICE:
2677 case ISN_CATCH:
2678 case ISN_CEXPR_AUCMD:
2679 case ISN_CHECKLEN:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002680 case ISN_CLEARDICT:
2681 case ISN_CMDMOD_REV:
2682 case ISN_COMPAREANY:
2683 case ISN_COMPAREBLOB:
2684 case ISN_COMPAREBOOL:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002685 case ISN_COMPARECLASS:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002686 case ISN_COMPAREDICT:
2687 case ISN_COMPAREFLOAT:
2688 case ISN_COMPAREFUNC:
2689 case ISN_COMPARELIST:
2690 case ISN_COMPARENR:
Bram Moolenaar7a222242022-03-01 19:23:24 +00002691 case ISN_COMPARENULL:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002692 case ISN_COMPAREOBJECT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002693 case ISN_COMPARESPECIAL:
2694 case ISN_COMPARESTRING:
2695 case ISN_CONCAT:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002696 case ISN_CONSTRUCT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002697 case ISN_COND2BOOL:
2698 case ISN_DEBUG:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002699 case ISN_DEFER:
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00002700 case ISN_DEFEROBJ:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002701 case ISN_DROP:
2702 case ISN_ECHO:
2703 case ISN_ECHOCONSOLE:
2704 case ISN_ECHOERR:
2705 case ISN_ECHOMSG:
Bram Moolenaar68a635a2022-09-01 17:26:17 +01002706 case ISN_ECHOWINDOW:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002707 case ISN_ENDLOOP:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002708 case ISN_ENDTRY:
2709 case ISN_EXECCONCAT:
2710 case ISN_EXECUTE:
2711 case ISN_FINALLY:
2712 case ISN_FINISH:
2713 case ISN_FOR:
2714 case ISN_GETITEM:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002715 case ISN_GET_OBJ_MEMBER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002716 case ISN_JUMP:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002717 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002718 case ISN_JUMP_IF_ARG_SET:
2719 case ISN_LISTAPPEND:
2720 case ISN_LISTINDEX:
2721 case ISN_LISTSLICE:
2722 case ISN_LOAD:
2723 case ISN_LOADBDICT:
2724 case ISN_LOADGDICT:
2725 case ISN_LOADOUTER:
2726 case ISN_LOADREG:
2727 case ISN_LOADTDICT:
2728 case ISN_LOADV:
2729 case ISN_LOADWDICT:
2730 case ISN_LOCKCONST:
2731 case ISN_MEMBER:
2732 case ISN_NEGATENR:
2733 case ISN_NEWDICT:
2734 case ISN_NEWLIST:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002735 case ISN_NEWPARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002736 case ISN_OPANY:
2737 case ISN_OPFLOAT:
2738 case ISN_OPNR:
2739 case ISN_PCALL:
2740 case ISN_PCALL_END:
2741 case ISN_PROF_END:
2742 case ISN_PROF_START:
2743 case ISN_PUSHBOOL:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002744 case ISN_PUSHCHANNEL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002745 case ISN_PUSHF:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002746 case ISN_PUSHJOB:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002747 case ISN_PUSHNR:
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002748 case ISN_PUSHOBJ:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002749 case ISN_PUSHSPEC:
2750 case ISN_PUT:
2751 case ISN_REDIREND:
2752 case ISN_REDIRSTART:
2753 case ISN_RETURN:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002754 case ISN_RETURN_OBJECT:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002755 case ISN_RETURN_VOID:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002756 case ISN_SHUFFLE:
2757 case ISN_SLICE:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002758 case ISN_SOURCE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002759 case ISN_STORE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002760 case ISN_STORENR:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002761 case ISN_STOREOUTER:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002762 case ISN_STORE_THIS:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002763 case ISN_STORERANGE:
2764 case ISN_STOREREG:
2765 case ISN_STOREV:
2766 case ISN_STRINDEX:
2767 case ISN_STRSLICE:
2768 case ISN_THROW:
2769 case ISN_TRYCONT:
2770 case ISN_UNLETINDEX:
2771 case ISN_UNLETRANGE:
2772 case ISN_UNPACK:
2773 case ISN_USEDICT:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002774 case ISN_WHILE:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002775 // nothing allocated
2776 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002777 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002778}
2779
2780 void
2781clear_instr_ga(garray_T *gap)
2782{
2783 int idx;
2784
2785 for (idx = 0; idx < gap->ga_len; ++idx)
2786 delete_instr(((isn_T *)gap->ga_data) + idx);
2787 ga_clear(gap);
2788}
2789
2790
2791#endif // defined(FEAT_EVAL)