blob: 5059eb0e627dab6bd4bb25554c08f9d8ad62d722 [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:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +0200243 case VAR_TYPEALIAS:
Bram Moolenaar078a4612022-01-04 15:17:03 +0000244 to_string_error(type->tt_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000245 return FAIL;
246 }
247
Bram Moolenaar078a4612022-01-04 15:17:03 +0000248 set_type_on_stack(cctx, &t_string, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000249 if ((isn = generate_instr(cctx, isntype)) == NULL)
250 return FAIL;
251 isn->isn_arg.tostring.offset = offset;
252 isn->isn_arg.tostring.tolerant = tolerant;
253
254 return OK;
255}
256
257 static int
Ernie Raele75fde62023-12-21 17:18:54 +0100258check_number_or_float(type_T *typ1, type_T *typ2, char_u *op)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000259{
Ernie Raele75fde62023-12-21 17:18:54 +0100260 vartype_T type1 = typ1->tt_type;
261 vartype_T type2 = typ2->tt_type;
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000262 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT
263 || type1 == VAR_ANY || type1 == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000264 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000265 || type2 == VAR_ANY || type2 == VAR_UNKNOWN)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000266 {
Ernie Raele75fde62023-12-21 17:18:54 +0100267 if (check_type_is_value(typ1) == FAIL
268 || check_type_is_value(typ2) == FAIL)
269 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000270 if (*op == '+')
271 emsg(_(e_wrong_argument_type_for_plus));
272 else
273 semsg(_(e_char_requires_number_or_float_arguments), *op);
274 return FAIL;
275 }
276 return OK;
277}
278
279/*
280 * Generate instruction for "+". For a list this creates a new list.
281 */
282 int
283generate_add_instr(
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000284 cctx_T *cctx,
285 vartype_T vartype,
286 type_T *type1,
287 type_T *type2,
288 exprtype_T expr_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000289{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000290 isn_T *isn = generate_instr_drop(cctx,
291 vartype == VAR_NUMBER ? ISN_OPNR
292 : vartype == VAR_LIST ? ISN_ADDLIST
293 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000294 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000295 : ISN_OPANY, 1);
296
297 if (vartype != VAR_LIST && vartype != VAR_BLOB
298 && type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000299 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000300 && type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000301 && type2->tt_type != VAR_UNKNOWN
Ernie Raele75fde62023-12-21 17:18:54 +0100302 && check_number_or_float(type1, type2, (char_u *)"+") == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000303 return FAIL;
304
305 if (isn != NULL)
306 {
307 if (isn->isn_type == ISN_ADDLIST)
308 isn->isn_arg.op.op_type = expr_type;
309 else
310 isn->isn_arg.op.op_type = EXPR_ADD;
311 }
312
313 // When concatenating two lists with different member types the member type
314 // becomes "any".
315 if (vartype == VAR_LIST
316 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
317 && type1->tt_member != type2->tt_member)
Bram Moolenaar078a4612022-01-04 15:17:03 +0000318 set_type_on_stack(cctx, &t_list_any, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000319
320 return isn == NULL ? FAIL : OK;
321}
322
323/*
324 * Get the type to use for an instruction for an operation on "type1" and
325 * "type2". If they are matching use a type-specific instruction. Otherwise
326 * fall back to runtime type checking.
327 */
328 vartype_T
329operator_type(type_T *type1, type_T *type2)
330{
331 if (type1->tt_type == type2->tt_type
332 && (type1->tt_type == VAR_NUMBER
333 || type1->tt_type == VAR_LIST
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000334 || type1->tt_type == VAR_FLOAT
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000335 || type1->tt_type == VAR_BLOB))
336 return type1->tt_type;
337 return VAR_ANY;
338}
339
340/*
341 * Generate an instruction with two arguments. The instruction depends on the
342 * type of the arguments.
343 */
344 int
345generate_two_op(cctx_T *cctx, char_u *op)
346{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000347 type_T *type1;
348 type_T *type2;
349 vartype_T vartype;
350 isn_T *isn;
351
352 RETURN_OK_IF_SKIP(cctx);
353
354 // Get the known type of the two items on the stack.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000355 type1 = get_type_on_stack(cctx, 1);
356 type2 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000357 vartype = operator_type(type1, type2);
358
359 switch (*op)
360 {
361 case '+':
362 if (generate_add_instr(cctx, vartype, type1, type2,
363 EXPR_COPY) == FAIL)
364 return FAIL;
365 break;
366
367 case '-':
368 case '*':
Ernie Raele75fde62023-12-21 17:18:54 +0100369 case '/': if (check_number_or_float(type1, type2, op) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000370 return FAIL;
371 if (vartype == VAR_NUMBER)
372 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000373 else if (vartype == VAR_FLOAT)
374 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000375 else
376 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
377 if (isn != NULL)
378 isn->isn_arg.op.op_type = *op == '*'
379 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
380 break;
381
382 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000383 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000384 && type1->tt_type != VAR_NUMBER)
385 || (type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000386 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000387 && type2->tt_type != VAR_NUMBER))
388 {
389 emsg(_(e_percent_requires_number_arguments));
390 return FAIL;
391 }
392 isn = generate_instr_drop(cctx,
393 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
394 if (isn != NULL)
395 isn->isn_arg.op.op_type = EXPR_REM;
396 break;
397 }
398
399 // correct type of result
400 if (vartype == VAR_ANY)
401 {
402 type_T *type = &t_any;
403
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000404 // float+number and number+float results in float
405 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100406 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000407 type = &t_float;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000408 set_type_on_stack(cctx, type, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000409 }
410
411 return OK;
412}
413
414/*
Ernie Raele75fde62023-12-21 17:18:54 +0100415 * Choose correct error message for the specified type information.
416 */
417 static isntype_T
418compare_isn_not_values(typval_T *tv, type_T *type)
419{
420 if (tv != NULL)
421 check_typval_is_value(tv);
422 else
423 check_type_is_value(type);
424 return ISN_DROP;
425}
426
427/*
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000428 * Get the instruction to use for comparing two values with specified types.
429 * Either "tv1" and "tv2" are passed or "type1" and "type2".
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000430 * Return ISN_DROP when failed.
431 */
432 static isntype_T
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000433get_compare_isn(
Bram Moolenaar2ed57ac2023-04-01 22:05:38 +0100434 exprtype_T exprtype,
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000435 typval_T *tv1,
436 typval_T *tv2,
437 type_T *type1,
438 type_T *type2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000439{
440 isntype_T isntype = ISN_DROP;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000441 vartype_T vartype1 = tv1 != NULL ? tv1->v_type : type1->tt_type;
442 vartype_T vartype2 = tv2 != NULL ? tv2->v_type : type2->tt_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000443
Ernie Raele75fde62023-12-21 17:18:54 +0100444 if (vartype1 == VAR_CLASS || vartype1 == VAR_TYPEALIAS)
445 return compare_isn_not_values(tv1, type1);
446 if (vartype2 == VAR_CLASS || vartype2 == VAR_TYPEALIAS)
447 return compare_isn_not_values(tv2, type2);
448
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000449 if (vartype1 == vartype2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000450 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000451 switch (vartype1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000452 {
453 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
454 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
455 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
456 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
457 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
458 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
459 case VAR_LIST: isntype = ISN_COMPARELIST; break;
460 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
461 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000462 case VAR_OBJECT: isntype = ISN_COMPAREOBJECT; break;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000463 default: isntype = ISN_COMPAREANY; break;
464 }
465 }
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000466 else if (vartype1 == VAR_ANY || vartype2 == VAR_ANY
467 || ((vartype1 == VAR_NUMBER || vartype1 == VAR_FLOAT)
468 && (vartype2 == VAR_NUMBER || vartype2 == VAR_FLOAT))
469 || (vartype1 == VAR_FUNC && vartype2 == VAR_PARTIAL)
470 || (vartype1 == VAR_PARTIAL && vartype2 == VAR_FUNC))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000471 isntype = ISN_COMPAREANY;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000472 else if (vartype1 == VAR_SPECIAL || vartype2 == VAR_SPECIAL)
Bram Moolenaar7a222242022-03-01 19:23:24 +0000473 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000474 if ((vartype1 == VAR_SPECIAL
475 && (tv1 != NULL ? tv1->vval.v_number == VVAL_NONE
476 : type1 == &t_none)
477 && vartype2 != VAR_STRING)
478 || (vartype2 == VAR_SPECIAL
479 && (tv2 != NULL ? tv2->vval.v_number == VVAL_NONE
480 : type2 == &t_none)
481 && vartype1 != VAR_STRING))
482 {
483 semsg(_(e_cannot_compare_str_with_str),
484 vartype_name(vartype1), vartype_name(vartype2));
485 return ISN_DROP;
486 }
Bram Moolenaar05667812022-03-15 20:21:33 +0000487 // although comparing null with number, float or bool is not useful, we
488 // allow it
Bram Moolenaar7a222242022-03-01 19:23:24 +0000489 isntype = ISN_COMPARENULL;
490 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000491
492 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
493 && (isntype == ISN_COMPAREBOOL
494 || isntype == ISN_COMPARESPECIAL
495 || isntype == ISN_COMPARENR
496 || isntype == ISN_COMPAREFLOAT))
497 {
498 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000499 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(vartype1));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000500 return ISN_DROP;
501 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000502 if (!(exprtype == EXPR_IS || exprtype == EXPR_ISNOT
503 || exprtype == EXPR_EQUAL || exprtype == EXPR_NEQUAL)
Ernie Raele75fde62023-12-21 17:18:54 +0100504 && (isntype == ISN_COMPAREOBJECT))
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000505 {
506 semsg(_(e_invalid_operation_for_str), vartype_name(vartype1));
507 return ISN_DROP;
508 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000509 if (isntype == ISN_DROP
Bram Moolenaar2ed57ac2023-04-01 22:05:38 +0100510 || (isntype != ISN_COMPARENULL
511 && (((exprtype != EXPR_EQUAL
512 && exprtype != EXPR_NEQUAL
513 && (vartype1 == VAR_BOOL || vartype1 == VAR_SPECIAL
514 || vartype2 == VAR_BOOL || vartype2 == VAR_SPECIAL)))
515 || ((exprtype != EXPR_EQUAL
516 && exprtype != EXPR_NEQUAL
517 && exprtype != EXPR_IS
518 && exprtype != EXPR_ISNOT
519 && (vartype1 == VAR_BLOB || vartype2 == VAR_BLOB
520 || vartype1 == VAR_LIST || vartype2 == VAR_LIST))))))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000521 {
522 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000523 vartype_name(vartype1), vartype_name(vartype2));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000524 return ISN_DROP;
525 }
526 return isntype;
527}
528
529 int
530check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
531{
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000532 if (get_compare_isn(type, tv1, tv2, NULL, NULL) == ISN_DROP)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000533 return FAIL;
534 return OK;
535}
536
537/*
538 * Generate an ISN_COMPARE* instruction with a boolean result.
539 */
540 int
541generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
542{
543 isntype_T isntype;
544 isn_T *isn;
545 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000546
547 RETURN_OK_IF_SKIP(cctx);
548
549 // Get the known type of the two items on the stack. If they are matching
550 // use a type-specific instruction. Otherwise fall back to runtime type
551 // checking.
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000552 isntype = get_compare_isn(exprtype, NULL, NULL,
553 get_type_on_stack(cctx, 1), get_type_on_stack(cctx, 0));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000554 if (isntype == ISN_DROP)
555 return FAIL;
556
557 if ((isn = generate_instr(cctx, isntype)) == NULL)
558 return FAIL;
559 isn->isn_arg.op.op_type = exprtype;
560 isn->isn_arg.op.op_ic = ic;
561
562 // takes two arguments, puts one bool back
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100563 --stack->ga_len;
564 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000565
566 return OK;
567}
568
569/*
LemonBoy372bcce2022-04-25 12:43:20 +0100570 * Generate an ISN_CONCAT instruction.
571 * "count" is the number of stack elements to join together and it must be
572 * greater or equal to one.
573 * The caller ensures all the "count" elements on the stack have the right type.
574 */
575 int
576generate_CONCAT(cctx_T *cctx, int count)
577{
578 isn_T *isn;
579 garray_T *stack = &cctx->ctx_type_stack;
580
581 RETURN_OK_IF_SKIP(cctx);
582
LemonBoy372bcce2022-04-25 12:43:20 +0100583 if ((isn = generate_instr(cctx, ISN_CONCAT)) == NULL)
584 return FAIL;
585 isn->isn_arg.number = count;
586
587 // drop the argument types
588 stack->ga_len -= count - 1;
589
590 return OK;
591}
592
593/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000594 * Generate an ISN_2BOOL instruction.
595 * "offset" is the offset in the type stack.
596 */
597 int
598generate_2BOOL(cctx_T *cctx, int invert, int offset)
599{
600 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000601
602 RETURN_OK_IF_SKIP(cctx);
603 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
604 return FAIL;
605 isn->isn_arg.tobool.invert = invert;
606 isn->isn_arg.tobool.offset = offset;
607
608 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000609 set_type_on_stack(cctx, &t_bool, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000610
611 return OK;
612}
613
614/*
615 * Generate an ISN_COND2BOOL instruction.
616 */
617 int
618generate_COND2BOOL(cctx_T *cctx)
619{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000620 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100621 if (generate_instr(cctx, ISN_COND2BOOL) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000622 return FAIL;
623
624 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000625 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000626
627 return OK;
628}
629
630 int
631generate_TYPECHECK(
632 cctx_T *cctx,
633 type_T *expected,
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000634 int number_ok, // add TTFLAG_NUMBER_OK flag
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000635 int offset,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100636 int is_var,
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000637 int argidx)
638{
639 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000640
641 RETURN_OK_IF_SKIP(cctx);
642 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
643 return FAIL;
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000644 type_T *tt;
645 if (expected->tt_type == VAR_FLOAT && number_ok)
646 {
647 // always allocate, also for static types
648 tt = ALLOC_ONE(type_T);
649 if (tt != NULL)
650 {
651 *tt = *expected;
Bram Moolenaarc4b3f642022-12-30 10:36:34 +0000652 tt->tt_flags &= ~TTFLAG_STATIC;
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000653 tt->tt_flags |= TTFLAG_NUMBER_OK;
654 }
655 }
656 else
657 tt = alloc_type(expected);
658
659 isn->isn_arg.type.ct_type = tt;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000660 isn->isn_arg.type.ct_off = (int8_T)offset;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100661 isn->isn_arg.type.ct_is_var = is_var;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000662 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
663
664 // type becomes expected
Bram Moolenaar078a4612022-01-04 15:17:03 +0000665 set_type_on_stack(cctx, expected, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000666
667 return OK;
668}
669
670 int
671generate_SETTYPE(
672 cctx_T *cctx,
673 type_T *expected)
674{
675 isn_T *isn;
676
677 RETURN_OK_IF_SKIP(cctx);
678 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
679 return FAIL;
680 isn->isn_arg.type.ct_type = alloc_type(expected);
681 return OK;
682}
683
684/*
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000685 * Generate an ISN_PUSHOBJ instruction. Object is always NULL.
686 */
Ernie Rael5c018be2023-08-27 18:40:26 +0200687 int
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000688generate_PUSHOBJ(cctx_T *cctx)
689{
690 RETURN_OK_IF_SKIP(cctx);
Ernie Rael5c018be2023-08-27 18:40:26 +0200691 if (generate_instr_type(cctx, ISN_PUSHOBJ, &t_object) == NULL)
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000692 return FAIL;
693 return OK;
694}
695
696/*
697 * Generate an ISN_PUSHCLASS instruction. "class" can be NULL.
698 */
699 static int
700generate_PUSHCLASS(cctx_T *cctx, class_T *class)
701{
702 RETURN_OK_IF_SKIP(cctx);
703 isn_T *isn = generate_instr_type(cctx, ISN_PUSHCLASS,
704 class == NULL ? &t_any : &class->class_type);
705 if (isn == NULL)
706 return FAIL;
Bram Moolenaar30a84472023-02-27 08:07:14 +0000707 isn->isn_arg.classarg = class;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000708 if (class != NULL)
709 ++class->class_refcount;
710 return OK;
711}
712
713/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000714 * Generate a PUSH instruction for "tv".
715 * "tv" will be consumed or cleared.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000716 */
717 int
718generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
719{
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100720 switch (tv->v_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000721 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100722 case VAR_BOOL:
723 generate_PUSHBOOL(cctx, tv->vval.v_number);
724 break;
725 case VAR_SPECIAL:
726 generate_PUSHSPEC(cctx, tv->vval.v_number);
727 break;
728 case VAR_NUMBER:
729 generate_PUSHNR(cctx, tv->vval.v_number);
730 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100731 case VAR_FLOAT:
732 generate_PUSHF(cctx, tv->vval.v_float);
733 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100734 case VAR_BLOB:
735 generate_PUSHBLOB(cctx, tv->vval.v_blob);
736 tv->vval.v_blob = NULL;
737 break;
738 case VAR_LIST:
739 if (tv->vval.v_list != NULL)
740 iemsg("non-empty list constant not supported");
741 generate_NEWLIST(cctx, 0, TRUE);
742 break;
743 case VAR_DICT:
744 if (tv->vval.v_dict != NULL)
745 iemsg("non-empty dict constant not supported");
746 generate_NEWDICT(cctx, 0, TRUE);
747 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000748#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100749 case VAR_JOB:
750 if (tv->vval.v_job != NULL)
751 iemsg("non-null job constant not supported");
752 generate_PUSHJOB(cctx);
753 break;
754 case VAR_CHANNEL:
755 if (tv->vval.v_channel != NULL)
756 iemsg("non-null channel constant not supported");
757 generate_PUSHCHANNEL(cctx);
758 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000759#endif
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100760 case VAR_FUNC:
761 if (tv->vval.v_string != NULL)
762 iemsg("non-null function constant not supported");
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100763 generate_PUSHFUNC(cctx, NULL, &t_func_unknown, TRUE);
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100764 break;
765 case VAR_PARTIAL:
766 if (tv->vval.v_partial != NULL)
767 iemsg("non-null partial constant not supported");
768 if (generate_instr_type(cctx, ISN_NEWPARTIAL, &t_func_unknown)
769 == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000770 return FAIL;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100771 break;
772 case VAR_STRING:
773 generate_PUSHS(cctx, &tv->vval.v_string);
774 tv->vval.v_string = NULL;
775 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000776 case VAR_OBJECT:
777 if (tv->vval.v_object != NULL)
778 {
779 emsg(_(e_cannot_use_non_null_object));
780 return FAIL;
781 }
782 generate_PUSHOBJ(cctx);
783 break;
784 case VAR_CLASS:
785 generate_PUSHCLASS(cctx, tv->vval.v_class);
786 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100787 default:
788 siemsg("constant type %d not supported", tv->v_type);
789 clear_tv(tv);
790 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000791 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100792 tv->v_type = VAR_UNKNOWN;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000793 return OK;
794}
795
796/*
797 * Generate an ISN_PUSHNR instruction.
798 */
799 int
800generate_PUSHNR(cctx_T *cctx, varnumber_T number)
801{
802 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000803
804 RETURN_OK_IF_SKIP(cctx);
805 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
806 return FAIL;
807 isn->isn_arg.number = number;
808
809 if (number == 0 || number == 1)
810 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000811 set_type_on_stack(cctx, &t_number_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000812 return OK;
813}
814
815/*
816 * Generate an ISN_PUSHBOOL instruction.
817 */
818 int
819generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
820{
821 isn_T *isn;
822
823 RETURN_OK_IF_SKIP(cctx);
824 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
825 return FAIL;
826 isn->isn_arg.number = number;
827
828 return OK;
829}
830
831/*
832 * Generate an ISN_PUSHSPEC instruction.
833 */
834 int
835generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
836{
837 isn_T *isn;
838
839 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000840 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC,
841 number == VVAL_NULL ? &t_null : &t_none)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000842 return FAIL;
843 isn->isn_arg.number = number;
844
845 return OK;
846}
847
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000848/*
849 * Generate an ISN_PUSHF instruction.
850 */
851 int
852generate_PUSHF(cctx_T *cctx, float_T fnumber)
853{
854 isn_T *isn;
855
856 RETURN_OK_IF_SKIP(cctx);
857 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
858 return FAIL;
859 isn->isn_arg.fnumber = fnumber;
860
861 return OK;
862}
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000863
864/*
865 * Generate an ISN_PUSHS instruction.
866 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100867 * Note that if "str" is used in the instruction OK is returned and "*str" is
868 * not set to NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000869 */
870 int
871generate_PUSHS(cctx_T *cctx, char_u **str)
872{
873 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100874 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000875
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100876 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000877 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100878 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
879 ret = FAIL;
880 else
881 {
882 isn->isn_arg.string = str == NULL ? NULL : *str;
883 return OK;
884 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000885 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100886 if (str != NULL)
887 VIM_CLEAR(*str);
888 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000889}
890
891/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000892 * Generate an ISN_PUSHCHANNEL instruction. Channel is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000893 */
894 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000895generate_PUSHCHANNEL(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000896{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000897 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100898#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100899 if (generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000900 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000901 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100902#else
903 emsg(_(e_channel_job_feature_not_available));
904 return FAIL;
905#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000906}
907
908/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000909 * Generate an ISN_PUSHJOB instruction. Job is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000910 */
911 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000912generate_PUSHJOB(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000913{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000914 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100915#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100916 if (generate_instr_type(cctx, ISN_PUSHJOB, &t_job) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000917 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000918 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100919#else
920 emsg(_(e_channel_job_feature_not_available));
921 return FAIL;
922#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000923}
924
925/*
926 * Generate an ISN_PUSHBLOB instruction.
927 * Consumes "blob".
928 */
929 int
930generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
931{
932 isn_T *isn;
933
934 RETURN_OK_IF_SKIP(cctx);
935 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
936 return FAIL;
937 isn->isn_arg.blob = blob;
938
939 return OK;
940}
941
942/*
943 * Generate an ISN_PUSHFUNC instruction with name "name".
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100944 * When "may_prefix" is TRUE prefix "g:" unless "name" is script-local or
945 * autoload.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000946 */
947 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100948generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type, int may_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000949{
950 isn_T *isn;
951 char_u *funcname;
952
953 RETURN_OK_IF_SKIP(cctx);
954 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
955 return FAIL;
956 if (name == NULL)
957 funcname = NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100958 else if (!may_prefix
959 || *name == K_SPECIAL // script-local
Bram Moolenaard041f422022-01-12 19:54:00 +0000960 || vim_strchr(name, AUTOLOAD_CHAR) != NULL) // autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000961 funcname = vim_strsave(name);
962 else
963 {
964 funcname = alloc(STRLEN(name) + 3);
965 if (funcname != NULL)
966 {
967 STRCPY(funcname, "g:");
968 STRCPY(funcname + 2, name);
969 }
970 }
971
972 isn->isn_arg.string = funcname;
973 return OK;
974}
975
976/*
Bram Moolenaar06b77222022-01-25 15:51:56 +0000977 * Generate an ISN_AUTOLOAD instruction.
978 */
979 int
980generate_AUTOLOAD(cctx_T *cctx, char_u *name, type_T *type)
981{
982 isn_T *isn;
983
984 RETURN_OK_IF_SKIP(cctx);
985 if ((isn = generate_instr_type(cctx, ISN_AUTOLOAD, type)) == NULL)
986 return FAIL;
987 isn->isn_arg.string = vim_strsave(name);
988 if (isn->isn_arg.string == NULL)
989 return FAIL;
990 return OK;
991}
992
993/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000994 * Generate an ISN_GETITEM instruction with "index".
995 * "with_op" is TRUE for "+=" and other operators, the stack has the current
996 * value below the list with values.
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100997 * Caller must check the type is a list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000998 */
999 int
1000generate_GETITEM(cctx_T *cctx, int index, int with_op)
1001{
1002 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001003 type_T *type = get_type_on_stack(cctx, with_op ? 1 : 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001004 type_T *item_type = &t_any;
1005
1006 RETURN_OK_IF_SKIP(cctx);
1007
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001008 item_type = type->tt_member;
1009 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1010 return FAIL;
1011 isn->isn_arg.getitem.gi_index = index;
1012 isn->isn_arg.getitem.gi_with_op = with_op;
1013
1014 // add the item type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001015 return push_type_stack(cctx, item_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001016}
1017
1018/*
1019 * Generate an ISN_SLICE instruction with "count".
1020 */
1021 int
1022generate_SLICE(cctx_T *cctx, int count)
1023{
1024 isn_T *isn;
1025
1026 RETURN_OK_IF_SKIP(cctx);
1027 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1028 return FAIL;
1029 isn->isn_arg.number = count;
1030 return OK;
1031}
1032
1033/*
1034 * Generate an ISN_CHECKLEN instruction with "min_len".
1035 */
1036 int
1037generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1038{
1039 isn_T *isn;
1040
1041 RETURN_OK_IF_SKIP(cctx);
1042
1043 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1044 return FAIL;
1045 isn->isn_arg.checklen.cl_min_len = min_len;
1046 isn->isn_arg.checklen.cl_more_OK = more_OK;
1047
1048 return OK;
1049}
1050
1051/*
1052 * Generate an ISN_STORE instruction.
1053 */
1054 int
1055generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1056{
1057 isn_T *isn;
1058
1059 RETURN_OK_IF_SKIP(cctx);
1060 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1061 return FAIL;
1062 if (name != NULL)
1063 isn->isn_arg.string = vim_strsave(name);
1064 else
1065 isn->isn_arg.number = idx;
1066
1067 return OK;
1068}
1069
1070/*
Bram Moolenaard505d172022-12-18 21:42:55 +00001071 * Generate an ISN_LOAD_CLASSMEMBER ("load" == TRUE) or ISN_STORE_CLASSMEMBER
1072 * ("load" == FALSE) instruction.
1073 */
1074 int
1075generate_CLASSMEMBER(
1076 cctx_T *cctx,
1077 int load,
1078 class_T *cl,
1079 int idx)
1080{
1081 isn_T *isn;
1082
1083 RETURN_OK_IF_SKIP(cctx);
1084 if (load)
1085 {
1086 ocmember_T *m = &cl->class_class_members[idx];
1087 isn = generate_instr_type(cctx, ISN_LOAD_CLASSMEMBER, m->ocm_type);
1088 }
1089 else
1090 {
1091 isn = generate_instr_drop(cctx, ISN_STORE_CLASSMEMBER, 1);
1092 }
1093 if (isn == NULL)
1094 return FAIL;
1095 isn->isn_arg.classmember.cm_class = cl;
1096 ++cl->class_refcount;
1097 isn->isn_arg.classmember.cm_idx = idx;
1098
1099 return OK;
1100}
1101
1102/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001103 * Generate an ISN_STOREOUTER instruction.
1104 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +00001105 static int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001106generate_STOREOUTER(cctx_T *cctx, int idx, int level, int loop_idx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001107{
1108 isn_T *isn;
1109
1110 RETURN_OK_IF_SKIP(cctx);
1111 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1112 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001113 if (level == 1 && loop_idx >= 0 && idx >= loop_idx)
1114 {
1115 // Store a variable defined in a loop. A copy will be made at the end
1116 // of the loop. TODO: how about deeper nesting?
1117 isn->isn_arg.outer.outer_idx = idx - loop_idx;
1118 isn->isn_arg.outer.outer_depth = OUTER_LOOP_DEPTH;
1119 }
1120 else
1121 {
1122 isn->isn_arg.outer.outer_idx = idx;
1123 isn->isn_arg.outer.outer_depth = level;
1124 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001125
1126 return OK;
1127}
1128
1129/*
1130 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1131 */
1132 int
1133generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1134{
1135 isn_T *isn;
1136
1137 RETURN_OK_IF_SKIP(cctx);
1138 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1139 return FAIL;
1140 isn->isn_arg.storenr.stnr_idx = idx;
1141 isn->isn_arg.storenr.stnr_val = value;
1142
1143 return OK;
1144}
1145
1146/*
1147 * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction
1148 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +00001149 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001150generate_STOREOPT(
1151 cctx_T *cctx,
1152 isntype_T isn_type,
1153 char_u *name,
1154 int opt_flags)
1155{
1156 isn_T *isn;
1157
1158 RETURN_OK_IF_SKIP(cctx);
1159 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1160 return FAIL;
1161 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1162 isn->isn_arg.storeopt.so_flags = opt_flags;
1163
1164 return OK;
1165}
1166
1167/*
1168 * Generate an ISN_LOAD or similar instruction.
1169 */
1170 int
1171generate_LOAD(
1172 cctx_T *cctx,
1173 isntype_T isn_type,
1174 int idx,
1175 char_u *name,
1176 type_T *type)
1177{
1178 isn_T *isn;
1179
1180 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001181 if ((isn = generate_instr_type2(cctx, isn_type, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001182 return FAIL;
1183 if (name != NULL)
1184 isn->isn_arg.string = vim_strsave(name);
1185 else
1186 isn->isn_arg.number = idx;
1187
1188 return OK;
1189}
1190
1191/*
1192 * Generate an ISN_LOADOUTER instruction
1193 */
1194 int
1195generate_LOADOUTER(
1196 cctx_T *cctx,
1197 int idx,
1198 int nesting,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001199 int loop_depth,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001200 int loop_idx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001201 type_T *type)
1202{
1203 isn_T *isn;
1204
1205 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001206 if ((isn = generate_instr_type2(cctx, ISN_LOADOUTER, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001207 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001208 if (nesting == 1 && loop_idx >= 0 && idx >= loop_idx)
1209 {
1210 // Load a variable defined in a loop. A copy will be made at the end
Bram Moolenaarcc341812022-09-19 15:54:34 +01001211 // of the loop.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001212 isn->isn_arg.outer.outer_idx = idx - loop_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001213 isn->isn_arg.outer.outer_depth = -loop_depth - 1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001214 }
1215 else
1216 {
1217 isn->isn_arg.outer.outer_idx = idx;
1218 isn->isn_arg.outer.outer_depth = nesting;
1219 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001220
1221 return OK;
1222}
1223
1224/*
1225 * Generate an ISN_LOADV instruction for v:var.
1226 */
1227 int
1228generate_LOADV(
1229 cctx_T *cctx,
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001230 char_u *name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001231{
1232 int di_flags;
1233 int vidx = find_vim_var(name, &di_flags);
1234 type_T *type;
1235
1236 RETURN_OK_IF_SKIP(cctx);
1237 if (vidx < 0)
1238 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001239 semsg(_(e_variable_not_found_str), name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001240 return FAIL;
1241 }
Bram Moolenaard787e402021-12-24 21:36:12 +00001242 type = get_vim_var_type(vidx, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001243 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
1244}
1245
1246/*
1247 * Generate an ISN_UNLET instruction.
1248 */
1249 int
1250generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
1251{
1252 isn_T *isn;
1253
1254 RETURN_OK_IF_SKIP(cctx);
1255 if ((isn = generate_instr(cctx, isn_type)) == NULL)
1256 return FAIL;
1257 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1258 isn->isn_arg.unlet.ul_forceit = forceit;
1259
1260 return OK;
1261}
1262
1263/*
1264 * Generate an ISN_LOCKCONST instruction.
1265 */
1266 int
1267generate_LOCKCONST(cctx_T *cctx)
1268{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001269 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01001270 if (generate_instr(cctx, ISN_LOCKCONST) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001271 return FAIL;
1272 return OK;
1273}
1274
1275/*
1276 * Generate an ISN_LOADS instruction.
1277 */
1278 int
1279generate_OLDSCRIPT(
1280 cctx_T *cctx,
1281 isntype_T isn_type,
1282 char_u *name,
1283 int sid,
1284 type_T *type)
1285{
1286 isn_T *isn;
1287
1288 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001289 if (isn_type == ISN_LOADS || isn_type == ISN_LOADEXPORT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001290 isn = generate_instr_type(cctx, isn_type, type);
1291 else
1292 isn = generate_instr_drop(cctx, isn_type, 1);
1293 if (isn == NULL)
1294 return FAIL;
1295 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1296 isn->isn_arg.loadstore.ls_sid = sid;
1297
1298 return OK;
1299}
1300
1301/*
1302 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1303 */
1304 int
1305generate_VIM9SCRIPT(
1306 cctx_T *cctx,
1307 isntype_T isn_type,
1308 int sid,
1309 int idx,
1310 type_T *type)
1311{
1312 isn_T *isn;
1313 scriptref_T *sref;
1314 scriptitem_T *si = SCRIPT_ITEM(sid);
1315
1316 RETURN_OK_IF_SKIP(cctx);
1317 if (isn_type == ISN_LOADSCRIPT)
Bram Moolenaar160afdb2022-02-06 17:17:02 +00001318 isn = generate_instr_type2(cctx, isn_type, type, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001319 else
1320 isn = generate_instr_drop(cctx, isn_type, 1);
1321 if (isn == NULL)
1322 return FAIL;
1323
1324 // This requires three arguments, which doesn't fit in an instruction, thus
1325 // we need to allocate a struct for this.
1326 sref = ALLOC_ONE(scriptref_T);
1327 if (sref == NULL)
1328 return FAIL;
1329 isn->isn_arg.script.scriptref = sref;
1330 sref->sref_sid = sid;
1331 sref->sref_idx = idx;
1332 sref->sref_seq = si->sn_script_seq;
1333 sref->sref_type = type;
1334 return OK;
1335}
1336
1337/*
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001338 * Generate an ISN_NEWLIST instruction for "count" items.
1339 * "use_null" is TRUE for null_list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001340 */
1341 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001342generate_NEWLIST(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001343{
1344 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001345 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001346 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001347 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001348
1349 RETURN_OK_IF_SKIP(cctx);
1350 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1351 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001352 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001353
Bram Moolenaar078a4612022-01-04 15:17:03 +00001354 // Get the member type and the declared member type from all the items on
1355 // the stack.
Ernie Raelfa831102023-12-14 20:06:39 +01001356 if ((member_type = get_member_type_from_stack(count, 1, cctx)) == NULL)
1357 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001358 type = get_list_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001359 decl_type = get_list_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001360
1361 // drop the value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001362 cctx->ctx_type_stack.ga_len -= count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001363
1364 // add the list type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001365 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001366}
1367
1368/*
1369 * Generate an ISN_NEWDICT instruction.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001370 * "use_null" is TRUE for null_dict.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001371 */
1372 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001373generate_NEWDICT(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001374{
1375 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001376 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001377 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001378 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001379
1380 RETURN_OK_IF_SKIP(cctx);
1381 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1382 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001383 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001384
Ernie Raelfa831102023-12-14 20:06:39 +01001385 if ((member_type = get_member_type_from_stack(count, 2, cctx)) == NULL)
1386 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001387 type = get_dict_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001388 decl_type = get_dict_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001389
1390 // drop the key and value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001391 cctx->ctx_type_stack.ga_len -= 2 * count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001392
1393 // add the dict type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001394 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001395}
1396
1397/*
1398 * Generate an ISN_FUNCREF instruction.
Bram Moolenaar313e4722023-02-08 20:55:27 +00001399 * For "obj.Method" "cl" is the class of the object (can be an interface or a
1400 * base class) and "fi" the index of the method on that class.
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001401 * "isn_idx" is set to the index of the instruction, so that fr_dfunc_idx can
1402 * be set later. The index is used instead of a pointer to the instruction
1403 * because the instruction memory can be reallocated.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001404 */
1405 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001406generate_FUNCREF(
1407 cctx_T *cctx,
1408 ufunc_T *ufunc,
Bram Moolenaar313e4722023-02-08 20:55:27 +00001409 class_T *cl,
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001410 int object_method,
Bram Moolenaar313e4722023-02-08 20:55:27 +00001411 int fi,
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001412 int *isn_idx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001413{
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001414 isn_T *isn;
1415 type_T *type;
1416 funcref_extra_T *extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001417 loopvarinfo_T loopinfo;
1418 int has_vars;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001419
1420 RETURN_OK_IF_SKIP(cctx);
1421 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1422 return FAIL;
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001423 if (isn_idx != NULL)
1424 // save the index of the new instruction
1425 *isn_idx = cctx->ctx_instr.ga_len - 1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001426
Bram Moolenaarcc341812022-09-19 15:54:34 +01001427 has_vars = get_loop_var_info(cctx, &loopinfo);
Bram Moolenaar313e4722023-02-08 20:55:27 +00001428 if (ufunc->uf_def_status == UF_NOT_COMPILED || has_vars || cl != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001429 {
1430 extra = ALLOC_CLEAR_ONE(funcref_extra_T);
1431 if (extra == NULL)
1432 return FAIL;
1433 isn->isn_arg.funcref.fr_extra = extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001434 extra->fre_loopvar_info = loopinfo;
Bram Moolenaar313e4722023-02-08 20:55:27 +00001435 if (cl != NULL)
1436 {
1437 extra->fre_class = cl;
1438 ++cl->class_refcount;
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001439 extra->fre_object_method = object_method;
Bram Moolenaar313e4722023-02-08 20:55:27 +00001440 extra->fre_method_idx = fi;
1441 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001442 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00001443 if (ufunc->uf_def_status == UF_NOT_COMPILED || cl != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001444 extra->fre_func_name = vim_strsave(ufunc->uf_name);
Bram Moolenaar313e4722023-02-08 20:55:27 +00001445 if (ufunc->uf_def_status != UF_NOT_COMPILED && cl == NULL)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001446 {
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001447 if (isn_idx == NULL && ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001448 // compile the function now, we need the uf_dfunc_idx value
1449 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001450 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001451 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01001452
1453 // Reserve an extra variable to keep track of the number of closures
1454 // created.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001455 cctx->ctx_has_closure = 1;
1456
1457 // If the referenced function is a closure, it may use items further up in
Bram Moolenaar139575d2022-03-15 19:29:30 +00001458 // the nested context, including this one. But not a function defined at
1459 // the script level.
1460 if ((ufunc->uf_flags & FC_CLOSURE)
1461 && func_name_refcount(cctx->ctx_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001462 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1463
Bram Moolenaar078a4612022-01-04 15:17:03 +00001464 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1465 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001466}
1467
1468/*
1469 * Generate an ISN_NEWFUNC instruction.
1470 * "lambda_name" and "func_name" must be in allocated memory and will be
1471 * consumed.
1472 */
1473 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001474generate_NEWFUNC(
1475 cctx_T *cctx,
1476 char_u *lambda_name,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001477 char_u *func_name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001478{
1479 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001480 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001481
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001482 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001483 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001484 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1485 ret = FAIL;
1486 else
1487 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001488 newfuncarg_T *arg = ALLOC_CLEAR_ONE(newfuncarg_T);
1489
1490 if (arg == NULL)
1491 ret = FAIL;
1492 else
1493 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01001494 // Reserve an extra variable to keep track of the number of
1495 // closures created.
1496 cctx->ctx_has_closure = 1;
1497
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001498 isn->isn_arg.newfunc.nf_arg = arg;
1499 arg->nfa_lambda = lambda_name;
1500 arg->nfa_global = func_name;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001501 (void)get_loop_var_info(cctx, &arg->nfa_loopvar_info);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001502 return OK;
1503 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001504 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001505 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001506 vim_free(lambda_name);
1507 vim_free(func_name);
1508 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001509}
1510
1511/*
1512 * Generate an ISN_DEF instruction: list functions
1513 */
1514 int
1515generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1516{
1517 isn_T *isn;
1518
1519 RETURN_OK_IF_SKIP(cctx);
1520 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1521 return FAIL;
1522 if (len > 0)
1523 {
1524 isn->isn_arg.string = vim_strnsave(name, len);
1525 if (isn->isn_arg.string == NULL)
1526 return FAIL;
1527 }
1528 return OK;
1529}
1530
1531/*
1532 * Generate an ISN_JUMP instruction.
1533 */
1534 int
1535generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1536{
1537 isn_T *isn;
1538 garray_T *stack = &cctx->ctx_type_stack;
1539
1540 RETURN_OK_IF_SKIP(cctx);
1541 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1542 return FAIL;
1543 isn->isn_arg.jump.jump_when = when;
1544 isn->isn_arg.jump.jump_where = where;
1545
1546 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1547 --stack->ga_len;
1548
1549 return OK;
1550}
1551
1552/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001553 * Generate an ISN_WHILE instruction. Similar to ISN_JUMP for :while
1554 */
1555 int
1556generate_WHILE(cctx_T *cctx, int funcref_idx)
1557{
1558 isn_T *isn;
1559 garray_T *stack = &cctx->ctx_type_stack;
1560
1561 RETURN_OK_IF_SKIP(cctx);
1562 if ((isn = generate_instr(cctx, ISN_WHILE)) == NULL)
1563 return FAIL;
1564 isn->isn_arg.whileloop.while_funcref_idx = funcref_idx;
1565 isn->isn_arg.whileloop.while_end = 0; // filled in later
1566
1567 if (stack->ga_len > 0)
1568 --stack->ga_len;
1569
1570 return OK;
1571}
1572
1573/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001574 * Generate an ISN_JUMP_IF_ARG_SET or ISN_JUMP_IF_ARG_NOT_SET instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001575 */
1576 int
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001577generate_JUMP_IF_ARG(cctx_T *cctx, isntype_T isn_type, int arg_off)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001578{
1579 isn_T *isn;
1580
1581 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001582 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001583 return FAIL;
1584 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1585 // jump_where is set later
1586 return OK;
1587}
1588
1589 int
1590generate_FOR(cctx_T *cctx, int loop_idx)
1591{
1592 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001593
1594 RETURN_OK_IF_SKIP(cctx);
1595 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1596 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001597 isn->isn_arg.forloop.for_loop_idx = loop_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001598
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001599 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001600 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001601}
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001602
1603 int
Bram Moolenaarcc341812022-09-19 15:54:34 +01001604generate_ENDLOOP(cctx_T *cctx, loop_info_T *loop_info)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001605{
1606 isn_T *isn;
1607
1608 RETURN_OK_IF_SKIP(cctx);
1609 if ((isn = generate_instr(cctx, ISN_ENDLOOP)) == NULL)
1610 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001611 isn->isn_arg.endloop.end_depth = loop_info->li_depth;
1612 isn->isn_arg.endloop.end_funcref_idx = loop_info->li_funcref_idx;
1613 isn->isn_arg.endloop.end_var_idx = loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001614 isn->isn_arg.endloop.end_var_count =
Bram Moolenaarcc341812022-09-19 15:54:34 +01001615 cctx->ctx_locals.ga_len - loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001616 return OK;
1617}
1618
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001619/*
1620 * Generate an ISN_TRYCONT instruction.
1621 */
1622 int
1623generate_TRYCONT(cctx_T *cctx, int levels, int where)
1624{
1625 isn_T *isn;
1626
1627 RETURN_OK_IF_SKIP(cctx);
1628 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1629 return FAIL;
1630 isn->isn_arg.trycont.tct_levels = levels;
1631 isn->isn_arg.trycont.tct_where = where;
1632
1633 return OK;
1634}
1635
Bram Moolenaar16900322022-09-08 19:51:45 +01001636/*
1637 * Check "argount" arguments and their types on the type stack.
1638 * Give an error and return FAIL if something is wrong.
1639 * When "method_call" is NULL no code is generated.
1640 */
1641 int
1642check_internal_func_args(
1643 cctx_T *cctx,
1644 int func_idx,
1645 int argcount,
1646 int method_call,
1647 type2_T **argtypes,
1648 type2_T *shuffled_argtypes)
1649{
1650 garray_T *stack = &cctx->ctx_type_stack;
1651 int argoff = check_internal_func(func_idx, argcount);
1652
1653 if (argoff < 0)
1654 return FAIL;
1655
1656 if (method_call && argoff > 1)
1657 {
Bram Moolenaarb7f22702023-04-27 16:24:07 +01001658 if (argcount < argoff)
1659 {
1660 semsg(_(e_not_enough_arguments_for_function_str),
1661 internal_func_name(func_idx));
1662 return FAIL;
1663 }
Bram Moolenaar16900322022-09-08 19:51:45 +01001664
Bram Moolenaarb7f22702023-04-27 16:24:07 +01001665 isn_T *isn = generate_instr(cctx, ISN_SHUFFLE);
Bram Moolenaar16900322022-09-08 19:51:45 +01001666 if (isn == NULL)
1667 return FAIL;
1668 isn->isn_arg.shuffle.shfl_item = argcount;
1669 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1670 }
1671
1672 if (argcount > 0)
1673 {
1674 type2_T *typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
1675
1676 // Check the types of the arguments.
1677 if (method_call && argoff > 1)
1678 {
1679 int i;
1680
1681 for (i = 0; i < argcount; ++i)
1682 shuffled_argtypes[i] = (i < argoff - 1)
1683 ? typep[i + 1]
1684 : (i == argoff - 1) ? typep[0] : typep[i];
1685 *argtypes = shuffled_argtypes;
1686 }
1687 else
1688 {
1689 int i;
1690
1691 for (i = 0; i < argcount; ++i)
1692 shuffled_argtypes[i] = typep[i];
1693 *argtypes = shuffled_argtypes;
1694 }
1695 if (internal_func_check_arg_types(*argtypes, func_idx, argcount,
1696 cctx) == FAIL)
1697 return FAIL;
1698 }
1699 return OK;
1700}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001701
1702/*
1703 * Generate an ISN_BCALL instruction.
1704 * "method_call" is TRUE for "value->method()"
1705 * Return FAIL if the number of arguments is wrong.
1706 */
1707 int
1708generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1709{
1710 isn_T *isn;
1711 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001712 type2_T *argtypes = NULL;
1713 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1714 type2_T *maptype = NULL;
1715 type_T *type;
Bram Moolenaar81330182022-02-01 12:11:58 +00001716 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001717
1718 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar16900322022-09-08 19:51:45 +01001719
1720 if (check_internal_func_args(cctx, func_idx, argcount, method_call,
1721 &argtypes, shuffled_argtypes) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001722 return FAIL;
1723
Bram Moolenaar16900322022-09-08 19:51:45 +01001724 if (internal_func_is_map(func_idx))
1725 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001726
1727 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1728 return FAIL;
1729 isn->isn_arg.bfunc.cbf_idx = func_idx;
1730 isn->isn_arg.bfunc.cbf_argcount = argcount;
1731
1732 // Drop the argument types and push the return type.
1733 stack->ga_len -= argcount;
Bram Moolenaar32517c42023-01-15 18:17:12 +00001734 type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type,
1735 cctx->ctx_type_list);
Bram Moolenaar81330182022-02-01 12:11:58 +00001736 if (push_type_stack2(cctx, type, decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001737 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001738
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001739 if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
1740 && maptype[0].type_decl->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001741 // Check that map() didn't change the item types.
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001742 generate_TYPECHECK(cctx, maptype[0].type_decl, FALSE, -1, FALSE, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001743
1744 return OK;
1745}
1746
1747/*
1748 * Generate an ISN_LISTAPPEND instruction. Works like add().
1749 * Argument count is already checked.
1750 */
1751 int
1752generate_LISTAPPEND(cctx_T *cctx)
1753{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001754 type_T *list_type;
1755 type_T *item_type;
1756 type_T *expected;
1757
1758 // Caller already checked that list_type is a list.
Bram Moolenaar81330182022-02-01 12:11:58 +00001759 // For checking the item type we use the declared type of the list and the
1760 // current type of the added item, adding a string to [1, 2] is OK.
1761 list_type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaarfa103972022-09-29 19:14:42 +01001762 if (arg_type_modifiable(list_type, 1) == FAIL)
1763 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001764 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001765 expected = list_type->tt_member;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001766 if (need_type(item_type, expected, FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001767 return FAIL;
1768
1769 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1770 return FAIL;
1771
Bram Moolenaar078a4612022-01-04 15:17:03 +00001772 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001773 return OK;
1774}
1775
1776/*
1777 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1778 * Argument count is already checked.
1779 */
1780 int
1781generate_BLOBAPPEND(cctx_T *cctx)
1782{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001783 type_T *item_type;
1784
Bram Moolenaarfa103972022-09-29 19:14:42 +01001785 // Caller already checked that blob_type is a blob, check it is modifiable.
1786 if (arg_type_modifiable(get_decl_type_on_stack(cctx, 1), 1) == FAIL)
1787 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001788 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001789 if (need_type(item_type, &t_number, FALSE,
1790 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001791 return FAIL;
1792
1793 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1794 return FAIL;
1795
Bram Moolenaar078a4612022-01-04 15:17:03 +00001796 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001797 return OK;
1798}
1799
1800/*
Bram Moolenaard0200c82023-01-28 15:19:40 +00001801 * Generate an ISN_DCALL, ISN_UCALL or ISN_METHODCALL instruction.
1802 * When calling a method on an object, of which we know the interface only,
1803 * then "cl" is the interface and "mi" the method index on the interface.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001804 * Return FAIL if the number of arguments is wrong.
1805 */
1806 int
Bram Moolenaard0200c82023-01-28 15:19:40 +00001807generate_CALL(
1808 cctx_T *cctx,
1809 ufunc_T *ufunc,
1810 class_T *cl,
1811 int mi,
1812 int pushed_argcount)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001813{
1814 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001815 int regular_args = ufunc->uf_args.ga_len;
1816 int argcount = pushed_argcount;
1817
1818 RETURN_OK_IF_SKIP(cctx);
1819 if (argcount > regular_args && !has_varargs(ufunc))
1820 {
1821 semsg(_(e_too_many_arguments_for_function_str),
1822 printable_func_name(ufunc));
1823 return FAIL;
1824 }
1825 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1826 {
1827 semsg(_(e_not_enough_arguments_for_function_str),
1828 printable_func_name(ufunc));
1829 return FAIL;
1830 }
1831
1832 if (ufunc->uf_def_status != UF_NOT_COMPILED
1833 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1834 {
1835 int i;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001836 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001837
1838 for (i = 0; i < argcount; ++i)
1839 {
1840 type_T *expected;
1841 type_T *actual;
1842
Bram Moolenaar078a4612022-01-04 15:17:03 +00001843 actual = get_type_on_stack(cctx, argcount - i - 1);
Ernie Raelb077b582023-12-14 20:11:44 +01001844 if (check_type_is_value(actual) == FAIL)
1845 return FAIL;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001846 if (actual->tt_type == VAR_SPECIAL
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001847 && i >= regular_args - ufunc->uf_def_args.ga_len)
1848 {
1849 // assume v:none used for default argument value
1850 continue;
1851 }
1852 if (i < regular_args)
1853 {
1854 if (ufunc->uf_arg_types == NULL)
1855 continue;
1856 expected = ufunc->uf_arg_types[i];
1857 }
1858 else if (ufunc->uf_va_type == NULL
1859 || ufunc->uf_va_type == &t_list_any)
1860 // possibly a lambda or "...: any"
1861 expected = &t_any;
1862 else
1863 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001864 if (need_type(actual, expected, FALSE,
1865 -argcount + i, i + 1, cctx, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001866 {
1867 arg_type_mismatch(expected, actual, i + 1);
1868 return FAIL;
1869 }
1870 }
Bram Moolenaar139575d2022-03-15 19:29:30 +00001871 compile_type = get_compile_type(ufunc);
1872 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001873 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaar139575d2022-03-15 19:29:30 +00001874 compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001875 return FAIL;
1876 }
1877 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1878 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001879 emsg_funcname(e_call_to_function_that_failed_to_compile_str,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001880 ufunc->uf_name);
1881 return FAIL;
1882 }
1883
Bram Moolenaard0200c82023-01-28 15:19:40 +00001884 if ((isn = generate_instr(cctx, cl != NULL ? ISN_METHODCALL
1885 : ufunc->uf_def_status != UF_NOT_COMPILED
1886 ? ISN_DCALL : ISN_UCALL)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001887 return FAIL;
Bram Moolenaar836137d2023-01-29 14:11:24 +00001888 if (cl != NULL /* isn->isn_type == ISN_METHODCALL */)
Bram Moolenaard0200c82023-01-28 15:19:40 +00001889 {
1890 isn->isn_arg.mfunc = ALLOC_ONE(cmfunc_T);
1891 if (isn->isn_arg.mfunc == NULL)
1892 return FAIL;
1893 isn->isn_arg.mfunc->cmf_itf = cl;
1894 ++cl->class_refcount;
1895 isn->isn_arg.mfunc->cmf_idx = mi;
1896 isn->isn_arg.mfunc->cmf_argcount = argcount;
1897 }
1898 else if (isn->isn_type == ISN_DCALL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001899 {
1900 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1901 isn->isn_arg.dfunc.cdf_argcount = argcount;
1902 }
1903 else
1904 {
1905 // A user function may be deleted and redefined later, can't use the
1906 // ufunc pointer, need to look it up again at runtime.
1907 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1908 isn->isn_arg.ufunc.cuf_argcount = argcount;
1909 }
1910
Bram Moolenaar078a4612022-01-04 15:17:03 +00001911 // drop the argument types
1912 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001913
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02001914 // For an object or class method call, drop the object/class type.
Yegappan Lakshmanan639751d2023-08-27 19:23:37 +02001915 if (ufunc->uf_class != NULL)
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02001916 {
1917 // When a class method is called without the class name prefix, then
1918 // the type will not be in the stack.
1919 type_T *stype = get_type_on_stack(cctx, 0);
1920 if (stype->tt_type == VAR_CLASS || stype->tt_type == VAR_OBJECT)
1921 cctx->ctx_type_stack.ga_len--;
1922 }
Yegappan Lakshmanan639751d2023-08-27 19:23:37 +02001923
Bram Moolenaar078a4612022-01-04 15:17:03 +00001924 // add return type
1925 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001926}
1927
1928/*
1929 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1930 */
1931 int
1932generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1933{
1934 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001935
1936 RETURN_OK_IF_SKIP(cctx);
1937 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1938 return FAIL;
1939 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1940 isn->isn_arg.ufunc.cuf_argcount = argcount;
1941
Bram Moolenaar078a4612022-01-04 15:17:03 +00001942 // drop the argument types
1943 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001944
Bram Moolenaar078a4612022-01-04 15:17:03 +00001945 // add return value
1946 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001947}
1948
1949/*
Bram Moolenaar16900322022-09-08 19:51:45 +01001950 * Check the arguments of function "type" against the types on the stack.
1951 * Returns OK or FAIL;
1952 */
1953 int
1954check_func_args_from_type(
1955 cctx_T *cctx,
1956 type_T *type,
1957 int argcount,
1958 int at_top,
1959 char_u *name)
1960{
1961 if (type->tt_argcount != -1)
1962 {
1963 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1964
1965 if (argcount < type->tt_min_argcount - varargs)
1966 {
1967 emsg_funcname(e_not_enough_arguments_for_function_str, name);
1968 return FAIL;
1969 }
1970 if (!varargs && argcount > type->tt_argcount)
1971 {
1972 emsg_funcname(e_too_many_arguments_for_function_str, name);
1973 return FAIL;
1974 }
1975 if (type->tt_args != NULL)
1976 {
1977 int i;
1978
1979 for (i = 0; i < argcount; ++i)
1980 {
1981 int offset = -argcount + i - (at_top ? 0 : 1);
1982 type_T *actual = get_type_on_stack(cctx, -1 - offset);
1983 type_T *expected;
1984
Ernie Raelb077b582023-12-14 20:11:44 +01001985 if (check_type_is_value(actual) == FAIL)
1986 return FAIL;
Bram Moolenaar16900322022-09-08 19:51:45 +01001987 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaar36818a92023-01-03 12:33:26 +00001988 {
1989 expected = type->tt_args[type->tt_argcount - 1];
1990 if (expected != NULL && expected->tt_type == VAR_LIST)
1991 expected = expected->tt_member;
1992 if (expected == NULL)
1993 expected = &t_any;
1994 }
Bram Moolenaar16900322022-09-08 19:51:45 +01001995 else if (i >= type->tt_min_argcount
1996 && actual->tt_type == VAR_SPECIAL)
1997 expected = &t_any;
1998 else
1999 expected = type->tt_args[i];
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002000 if (need_type(actual, expected, FALSE,
2001 offset, i + 1, cctx, TRUE, FALSE) == FAIL)
Bram Moolenaar16900322022-09-08 19:51:45 +01002002 {
2003 arg_type_mismatch(expected, actual, i + 1);
2004 return FAIL;
2005 }
2006 }
2007 }
2008 }
2009
2010 return OK;
2011}
2012/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002013 * Generate an ISN_PCALL instruction.
2014 * "type" is the type of the FuncRef.
2015 */
2016 int
2017generate_PCALL(
2018 cctx_T *cctx,
2019 int argcount,
2020 char_u *name,
2021 type_T *type,
2022 int at_top)
2023{
2024 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002025 type_T *ret_type;
2026
2027 RETURN_OK_IF_SKIP(cctx);
2028
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002029 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002030 ret_type = &t_any;
2031 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
2032 {
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002033 if (check_func_args_from_type(cctx, type, argcount, at_top, name)
2034 == FAIL)
Bram Moolenaar16900322022-09-08 19:51:45 +01002035 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002036
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002037 ret_type = type->tt_member;
2038 if (ret_type == &t_unknown)
2039 // return type not known yet, use a runtime check
2040 ret_type = &t_any;
2041 }
2042 else
2043 {
2044 semsg(_(e_not_callable_type_str), name);
2045 return FAIL;
2046 }
2047
2048 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2049 return FAIL;
2050 isn->isn_arg.pfunc.cpf_top = at_top;
2051 isn->isn_arg.pfunc.cpf_argcount = argcount;
2052
Bram Moolenaar078a4612022-01-04 15:17:03 +00002053 // drop the arguments and the funcref/partial
2054 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002055
Bram Moolenaar078a4612022-01-04 15:17:03 +00002056 // push the return value
2057 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002058
2059 // If partial is above the arguments it must be cleared and replaced with
2060 // the return value.
2061 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2062 return FAIL;
2063
2064 return OK;
2065}
2066
2067/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002068 * Generate an ISN_DEFER instruction.
2069 */
2070 int
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02002071generate_DEFER(cctx_T *cctx, int var_idx, int argcount)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002072{
2073 isn_T *isn;
2074
2075 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02002076 if ((isn = generate_instr_drop(cctx, ISN_DEFER, argcount + 1)) == NULL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002077 return FAIL;
2078 isn->isn_arg.defer.defer_var_idx = var_idx;
2079 isn->isn_arg.defer.defer_argcount = argcount;
2080 return OK;
2081}
2082
2083/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002084 * Generate an ISN_STRINGMEMBER instruction.
2085 */
2086 int
2087generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
2088{
2089 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002090 type_T *type;
2091
2092 RETURN_OK_IF_SKIP(cctx);
2093 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
2094 return FAIL;
2095 isn->isn_arg.string = vim_strnsave(name, len);
2096
2097 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00002098 type = get_type_on_stack(cctx, 0);
Bram Moolenaar0089ce22022-10-08 14:39:36 +01002099 if (type->tt_type != VAR_DICT
2100 && type->tt_type != VAR_ANY && type->tt_type != VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002101 {
2102 char *tofree;
2103
2104 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
2105 name, type_name(type, &tofree));
2106 vim_free(tofree);
2107 return FAIL;
2108 }
2109 // change dict type to dict member type
2110 if (type->tt_type == VAR_DICT)
2111 {
Bram Moolenaar0089ce22022-10-08 14:39:36 +01002112 type_T *ntype = type->tt_member->tt_type == VAR_UNKNOWN
Bram Moolenaar078a4612022-01-04 15:17:03 +00002113 ? &t_any : type->tt_member;
2114 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002115 }
2116
2117 return OK;
2118}
2119
2120/*
2121 * Generate an ISN_ECHO instruction.
2122 */
2123 int
2124generate_ECHO(cctx_T *cctx, int with_white, int count)
2125{
2126 isn_T *isn;
2127
2128 RETURN_OK_IF_SKIP(cctx);
2129 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2130 return FAIL;
2131 isn->isn_arg.echo.echo_with_white = with_white;
2132 isn->isn_arg.echo.echo_count = count;
2133
2134 return OK;
2135}
2136
2137/*
2138 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
2139 */
2140 int
2141generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
2142{
2143 isn_T *isn;
2144
Bram Moolenaar2eae3d22022-10-07 15:09:27 +01002145 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002146 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
2147 return FAIL;
2148 isn->isn_arg.number = count;
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002149 return OK;
2150}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002151
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002152/*
2153 * Generate an ISN_ECHOWINDOW instruction
2154 */
2155 int
2156generate_ECHOWINDOW(cctx_T *cctx, int count, long time)
2157{
2158 isn_T *isn;
2159
2160 if ((isn = generate_instr_drop(cctx, ISN_ECHOWINDOW, count)) == NULL)
2161 return FAIL;
2162 isn->isn_arg.echowin.ewin_count = count;
2163 isn->isn_arg.echowin.ewin_time = time;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002164 return OK;
2165}
2166
2167/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002168 * Generate an ISN_SOURCE instruction.
2169 */
2170 int
2171generate_SOURCE(cctx_T *cctx, int sid)
2172{
2173 isn_T *isn;
2174
2175 if ((isn = generate_instr(cctx, ISN_SOURCE)) == NULL)
2176 return FAIL;
2177 isn->isn_arg.number = sid;
2178
2179 return OK;
2180}
2181
2182/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002183 * Generate an ISN_PUT instruction.
2184 */
2185 int
2186generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2187{
2188 isn_T *isn;
2189
2190 RETURN_OK_IF_SKIP(cctx);
2191 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2192 return FAIL;
2193 isn->isn_arg.put.put_regname = regname;
2194 isn->isn_arg.put.put_lnum = lnum;
2195 return OK;
2196}
2197
2198/*
Ernie Rael64885642023-10-04 20:16:22 +02002199 * Generate a LOCKUNLOCK instruction.The root item, where the indexing starts
2200 * to find the variable, is on the stack. The instr takes
2201 * - the string to parse, "root.b[idx1][idx2].d.val", to find the variable
2202 * - the class, if any, in which the string executes.
2203 * - if the root item is a function argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002204 * A copy is made of "line".
2205 */
2206 int
Ernie Raelee865f32023-09-29 19:53:55 +02002207generate_LOCKUNLOCK(cctx_T *cctx, char_u *line, int is_arg)
2208{
2209 isn_T *isn;
2210
2211 RETURN_OK_IF_SKIP(cctx);
2212 if ((isn = generate_instr(cctx, ISN_LOCKUNLOCK)) == NULL)
2213 return FAIL;
Ernie Rael64885642023-10-04 20:16:22 +02002214 class_T *cl = cctx->ctx_ufunc != NULL ? cctx->ctx_ufunc->uf_class : NULL;
2215 isn->isn_arg.lockunlock.lu_string = vim_strsave(line);
2216 isn->isn_arg.lockunlock.lu_cl_exec = cl;
2217 if (cl != NULL)
2218 ++cl->class_refcount;
2219 isn->isn_arg.lockunlock.lu_is_arg = is_arg;
Ernie Raelee865f32023-09-29 19:53:55 +02002220 return OK;
2221}
2222
2223/*
2224 * Generate an EXEC instruction that takes a string argument.
2225 * A copy is made of "line".
2226 */
2227 int
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002228generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
2229{
2230 isn_T *isn;
2231
2232 RETURN_OK_IF_SKIP(cctx);
2233 if ((isn = generate_instr(cctx, isntype)) == NULL)
2234 return FAIL;
2235 isn->isn_arg.string = vim_strsave(line);
2236 return OK;
2237}
2238
2239/*
2240 * Generate an EXEC instruction that takes a string argument.
2241 * "str" must be allocated, it is consumed.
2242 */
2243 int
2244generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
2245{
2246 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002247 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002248
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002249 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002250 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002251 if ((isn = generate_instr(cctx, isntype)) == NULL)
2252 ret = FAIL;
2253 else
2254 {
2255 isn->isn_arg.string = str;
2256 return OK;
2257 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002258 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002259 vim_free(str);
2260 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002261}
2262
2263 int
2264generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2265{
2266 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002267
2268 RETURN_OK_IF_SKIP(cctx);
2269 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2270 return FAIL;
2271 isn->isn_arg.string = vim_strsave(line);
2272
Bram Moolenaar078a4612022-01-04 15:17:03 +00002273 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002274}
2275
2276 int
2277generate_EXECCONCAT(cctx_T *cctx, int count)
2278{
2279 isn_T *isn;
2280
2281 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2282 return FAIL;
2283 isn->isn_arg.number = count;
2284 return OK;
2285}
2286
2287/*
2288 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2289 */
2290 int
2291generate_RANGE(cctx_T *cctx, char_u *range)
2292{
2293 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002294
2295 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2296 return FAIL;
2297 isn->isn_arg.string = range;
2298
Bram Moolenaar078a4612022-01-04 15:17:03 +00002299 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002300}
2301
2302 int
2303generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2304{
2305 isn_T *isn;
2306
2307 RETURN_OK_IF_SKIP(cctx);
2308 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2309 return FAIL;
2310 isn->isn_arg.unpack.unp_count = var_count;
2311 isn->isn_arg.unpack.unp_semicolon = semicolon;
2312 return OK;
2313}
2314
2315/*
2316 * Generate an instruction for any command modifiers.
2317 */
2318 int
2319generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
2320{
2321 isn_T *isn;
2322
2323 if (has_cmdmod(cmod, FALSE))
2324 {
2325 cctx->ctx_has_cmdmod = TRUE;
2326
2327 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
2328 return FAIL;
2329 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2330 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2331 return FAIL;
2332 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
2333 // filter program now belongs to the instruction
2334 cmod->cmod_filter_regmatch.regprog = NULL;
2335 }
2336
2337 return OK;
2338}
2339
2340 int
2341generate_undo_cmdmods(cctx_T *cctx)
2342{
2343 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2344 return FAIL;
2345 cctx->ctx_has_cmdmod = FALSE;
2346 return OK;
2347}
2348
2349/*
2350 * Generate a STORE instruction for "dest", not being "dest_local".
Bram Moolenaard505d172022-12-18 21:42:55 +00002351 * "lhs" might be NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002352 * Return FAIL when out of memory.
2353 */
2354 int
2355generate_store_var(
2356 cctx_T *cctx,
2357 assign_dest_T dest,
2358 int opt_flags,
2359 int vimvaridx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002360 type_T *type,
Bram Moolenaard505d172022-12-18 21:42:55 +00002361 char_u *name,
2362 lhs_T *lhs)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002363{
2364 switch (dest)
2365 {
2366 case dest_option:
2367 return generate_STOREOPT(cctx, ISN_STOREOPT,
2368 skip_option_env_lead(name), opt_flags);
2369 case dest_func_option:
2370 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
2371 skip_option_env_lead(name), opt_flags);
2372 case dest_global:
2373 // include g: with the name, easier to execute that way
2374 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
2375 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
2376 case dest_buffer:
2377 // include b: with the name, easier to execute that way
2378 return generate_STORE(cctx, ISN_STOREB, 0, name);
2379 case dest_window:
2380 // include w: with the name, easier to execute that way
2381 return generate_STORE(cctx, ISN_STOREW, 0, name);
2382 case dest_tab:
2383 // include t: with the name, easier to execute that way
2384 return generate_STORE(cctx, ISN_STORET, 0, name);
2385 case dest_env:
2386 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
2387 case dest_reg:
2388 return generate_STORE(cctx, ISN_STOREREG,
2389 name[1] == '@' ? '"' : name[1], NULL);
2390 case dest_vimvar:
2391 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
2392 case dest_script:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002393 {
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002394 int scriptvar_idx = lhs->lhs_scriptvar_idx;
2395 int scriptvar_sid = lhs->lhs_scriptvar_sid;
2396 if (scriptvar_idx < 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002397 {
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002398 isntype_T isn_type = ISN_STORES;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002399
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002400 if (SCRIPT_ID_VALID(scriptvar_sid)
2401 && SCRIPT_ITEM(scriptvar_sid)->sn_import_autoload
2402 && SCRIPT_ITEM(scriptvar_sid)->sn_autoload_prefix
2403 == NULL)
2404 {
2405 // "import autoload './dir/script.vim'" - load script
2406 // first
2407 if (generate_SOURCE(cctx, scriptvar_sid) == FAIL)
2408 return FAIL;
2409 isn_type = ISN_STOREEXPORT;
2410 }
2411
2412 // "s:" may be included in the name.
2413 return generate_OLDSCRIPT(cctx, isn_type, name,
2414 scriptvar_sid, type);
2415 }
2416 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002417 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002418 }
Bram Moolenaard505d172022-12-18 21:42:55 +00002419 case dest_class_member:
2420 return generate_CLASSMEMBER(cctx, FALSE,
2421 lhs->lhs_class, lhs->lhs_classmember_idx);
2422
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002423 case dest_local:
2424 case dest_expr:
2425 // cannot happen
2426 break;
2427 }
2428 return FAIL;
2429}
2430
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002431/*
2432 * Return TRUE when inside a "for" or "while" loop.
2433 */
2434 int
2435inside_loop_scope(cctx_T *cctx)
2436{
2437 scope_T *scope = cctx->ctx_scope;
2438
2439 for (;;)
2440 {
2441 if (scope == NULL)
2442 break;
2443 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
2444 return TRUE;
2445 scope = scope->se_outer;
2446 }
2447 return FALSE;
2448}
2449
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002450 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002451generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002452{
2453 if (lhs->lhs_dest != dest_local)
2454 return generate_store_var(cctx, lhs->lhs_dest,
2455 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
Bram Moolenaard505d172022-12-18 21:42:55 +00002456 lhs->lhs_type, lhs->lhs_name, lhs);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002457
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002458 if (lhs->lhs_lvar == NULL)
2459 return OK;
2460
2461 garray_T *instr = &cctx->ctx_instr;
2462 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2463
2464 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
2465 // ISN_STORENR.
2466 // And "var = 0" does not need any instruction.
2467 if (lhs->lhs_lvar->lv_from_outer == 0
2468 && instr->ga_len == instr_count + 1
2469 && isn->isn_type == ISN_PUSHNR)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002470 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002471 varnumber_T val = isn->isn_arg.number;
2472 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002473
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002474 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002475 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002476 // zero is the default value, no need to do anything
2477 --instr->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002478 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002479 else
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002480 {
2481 isn->isn_type = ISN_STORENR;
2482 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
2483 isn->isn_arg.storenr.stnr_val = val;
2484 }
2485 if (stack->ga_len > 0)
2486 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002487 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002488 else if (lhs->lhs_lvar->lv_from_outer > 0)
2489 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
2490 lhs->lhs_lvar->lv_from_outer, lhs->lhs_lvar->lv_loop_idx);
2491 else
2492 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002493 return OK;
2494}
2495
2496#if defined(FEAT_PROFILE) || defined(PROTO)
2497 void
2498may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2499{
2500 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
2501 generate_instr(cctx, ISN_PROF_END);
2502}
2503#endif
2504
2505
2506/*
2507 * Delete an instruction, free what it contains.
2508 */
2509 void
2510delete_instr(isn_T *isn)
2511{
2512 switch (isn->isn_type)
2513 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002514 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002515 case ISN_DEF:
2516 case ISN_EXEC:
2517 case ISN_EXECRANGE:
2518 case ISN_EXEC_SPLIT:
2519 case ISN_LEGACY_EVAL:
2520 case ISN_LOADAUTO:
2521 case ISN_LOADB:
2522 case ISN_LOADENV:
2523 case ISN_LOADG:
2524 case ISN_LOADOPT:
2525 case ISN_LOADT:
2526 case ISN_LOADW:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002527 case ISN_PUSHEXC:
2528 case ISN_PUSHFUNC:
2529 case ISN_PUSHS:
2530 case ISN_RANGE:
2531 case ISN_STOREAUTO:
2532 case ISN_STOREB:
2533 case ISN_STOREENV:
2534 case ISN_STOREG:
2535 case ISN_STORET:
2536 case ISN_STOREW:
2537 case ISN_STRINGMEMBER:
2538 vim_free(isn->isn_arg.string);
2539 break;
2540
Ernie Rael64885642023-10-04 20:16:22 +02002541 case ISN_LOCKUNLOCK:
2542 class_unref(isn->isn_arg.lockunlock.lu_cl_exec);
2543 vim_free(isn->isn_arg.lockunlock.lu_string);
2544 break;
2545
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002546 case ISN_SUBSTITUTE:
2547 {
2548 int idx;
2549 isn_T *list = isn->isn_arg.subs.subs_instr;
2550
2551 vim_free(isn->isn_arg.subs.subs_cmd);
2552 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2553 delete_instr(list + idx);
2554 vim_free(list);
2555 }
2556 break;
2557
2558 case ISN_INSTR:
2559 {
2560 int idx;
2561 isn_T *list = isn->isn_arg.instr;
2562
2563 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2564 delete_instr(list + idx);
2565 vim_free(list);
2566 }
2567 break;
2568
2569 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002570 case ISN_LOADEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002571 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002572 case ISN_STOREEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002573 vim_free(isn->isn_arg.loadstore.ls_name);
2574 break;
2575
2576 case ISN_UNLET:
2577 case ISN_UNLETENV:
2578 vim_free(isn->isn_arg.unlet.ul_name);
2579 break;
2580
2581 case ISN_STOREOPT:
2582 case ISN_STOREFUNCOPT:
2583 vim_free(isn->isn_arg.storeopt.so_name);
2584 break;
2585
2586 case ISN_PUSHBLOB: // push blob isn_arg.blob
2587 blob_unref(isn->isn_arg.blob);
2588 break;
2589
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002590 case ISN_PUSHCLASS:
Bram Moolenaar30a84472023-02-27 08:07:14 +00002591 class_unref(isn->isn_arg.classarg);
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002592 break;
2593
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002594 case ISN_UCALL:
2595 vim_free(isn->isn_arg.ufunc.cuf_name);
2596 break;
2597
2598 case ISN_FUNCREF:
2599 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002600 funcref_T *funcref = &isn->isn_arg.funcref;
2601 funcref_extra_T *extra = funcref->fr_extra;
2602
2603 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002604 {
2605 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002606 + funcref->fr_dfunc_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002607 ufunc_T *ufunc = dfunc->df_ufunc;
2608
2609 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2610 func_ptr_unref(ufunc);
2611 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002612 if (extra != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002613 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002614 char_u *name = extra->fre_func_name;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002615
2616 if (name != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002617 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002618 func_unref(name);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002619 vim_free(name);
2620 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00002621 if (extra->fre_class != NULL)
2622 class_unref(extra->fre_class);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002623 vim_free(extra);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002624 }
2625 }
2626 break;
2627
2628 case ISN_DCALL:
2629 {
2630 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002631 + isn->isn_arg.dfunc.cdf_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002632
2633 if (dfunc->df_ufunc != NULL
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002634 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002635 func_ptr_unref(dfunc->df_ufunc);
2636 }
2637 break;
2638
Bram Moolenaard0200c82023-01-28 15:19:40 +00002639 case ISN_METHODCALL:
2640 {
2641 cmfunc_T *mfunc = isn->isn_arg.mfunc;
2642 class_unref(mfunc->cmf_itf);
2643 vim_free(mfunc);
2644 }
2645 break;
2646
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002647 case ISN_NEWFUNC:
2648 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002649 newfuncarg_T *arg = isn->isn_arg.newfunc.nf_arg;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002650
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002651 if (arg != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002652 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002653 ufunc_T *ufunc = find_func_even_dead(
2654 arg->nfa_lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002655
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002656 if (ufunc != NULL)
2657 {
2658 unlink_def_function(ufunc);
2659 func_ptr_unref(ufunc);
2660 }
2661
2662 vim_free(arg->nfa_lambda);
2663 vim_free(arg->nfa_global);
2664 vim_free(arg);
2665 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002666 }
2667 break;
2668
2669 case ISN_CHECKTYPE:
2670 case ISN_SETTYPE:
2671 free_type(isn->isn_arg.type.ct_type);
2672 break;
2673
2674 case ISN_CMDMOD:
2675 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002676 ->cmod_filter_regmatch.regprog);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002677 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2678 break;
2679
2680 case ISN_LOADSCRIPT:
2681 case ISN_STORESCRIPT:
2682 vim_free(isn->isn_arg.script.scriptref);
2683 break;
2684
Bram Moolenaard505d172022-12-18 21:42:55 +00002685 case ISN_LOAD_CLASSMEMBER:
2686 case ISN_STORE_CLASSMEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00002687 case ISN_GET_ITF_MEMBER:
Bram Moolenaard505d172022-12-18 21:42:55 +00002688 class_unref(isn->isn_arg.classmember.cm_class);
2689 break;
2690
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002691 case ISN_STOREINDEX:
2692 class_unref(isn->isn_arg.storeindex.si_class);
2693 break;
2694
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002695 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002696 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002697 break;
2698
2699 case ISN_CEXPR_CORE:
2700 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2701 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2702 break;
2703
2704 case ISN_2BOOL:
2705 case ISN_2STRING:
2706 case ISN_2STRING_ANY:
2707 case ISN_ADDBLOB:
2708 case ISN_ADDLIST:
2709 case ISN_ANYINDEX:
2710 case ISN_ANYSLICE:
2711 case ISN_BCALL:
2712 case ISN_BLOBAPPEND:
2713 case ISN_BLOBINDEX:
2714 case ISN_BLOBSLICE:
2715 case ISN_CATCH:
2716 case ISN_CEXPR_AUCMD:
2717 case ISN_CHECKLEN:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002718 case ISN_CLEARDICT:
2719 case ISN_CMDMOD_REV:
2720 case ISN_COMPAREANY:
2721 case ISN_COMPAREBLOB:
2722 case ISN_COMPAREBOOL:
2723 case ISN_COMPAREDICT:
2724 case ISN_COMPAREFLOAT:
2725 case ISN_COMPAREFUNC:
2726 case ISN_COMPARELIST:
2727 case ISN_COMPARENR:
Bram Moolenaar7a222242022-03-01 19:23:24 +00002728 case ISN_COMPARENULL:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002729 case ISN_COMPAREOBJECT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002730 case ISN_COMPARESPECIAL:
2731 case ISN_COMPARESTRING:
2732 case ISN_CONCAT:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002733 case ISN_CONSTRUCT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002734 case ISN_COND2BOOL:
2735 case ISN_DEBUG:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002736 case ISN_DEFER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002737 case ISN_DROP:
2738 case ISN_ECHO:
2739 case ISN_ECHOCONSOLE:
2740 case ISN_ECHOERR:
2741 case ISN_ECHOMSG:
Bram Moolenaar68a635a2022-09-01 17:26:17 +01002742 case ISN_ECHOWINDOW:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002743 case ISN_ENDLOOP:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002744 case ISN_ENDTRY:
2745 case ISN_EXECCONCAT:
2746 case ISN_EXECUTE:
2747 case ISN_FINALLY:
2748 case ISN_FINISH:
2749 case ISN_FOR:
2750 case ISN_GETITEM:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002751 case ISN_GET_OBJ_MEMBER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002752 case ISN_JUMP:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002753 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002754 case ISN_JUMP_IF_ARG_SET:
2755 case ISN_LISTAPPEND:
2756 case ISN_LISTINDEX:
2757 case ISN_LISTSLICE:
2758 case ISN_LOAD:
2759 case ISN_LOADBDICT:
2760 case ISN_LOADGDICT:
2761 case ISN_LOADOUTER:
2762 case ISN_LOADREG:
2763 case ISN_LOADTDICT:
2764 case ISN_LOADV:
2765 case ISN_LOADWDICT:
2766 case ISN_LOCKCONST:
2767 case ISN_MEMBER:
2768 case ISN_NEGATENR:
2769 case ISN_NEWDICT:
2770 case ISN_NEWLIST:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002771 case ISN_NEWPARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002772 case ISN_OPANY:
2773 case ISN_OPFLOAT:
2774 case ISN_OPNR:
2775 case ISN_PCALL:
2776 case ISN_PCALL_END:
2777 case ISN_PROF_END:
2778 case ISN_PROF_START:
2779 case ISN_PUSHBOOL:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002780 case ISN_PUSHCHANNEL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002781 case ISN_PUSHF:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002782 case ISN_PUSHJOB:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002783 case ISN_PUSHNR:
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002784 case ISN_PUSHOBJ:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002785 case ISN_PUSHSPEC:
2786 case ISN_PUT:
2787 case ISN_REDIREND:
2788 case ISN_REDIRSTART:
2789 case ISN_RETURN:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002790 case ISN_RETURN_OBJECT:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002791 case ISN_RETURN_VOID:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002792 case ISN_SHUFFLE:
2793 case ISN_SLICE:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002794 case ISN_SOURCE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002795 case ISN_STORE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002796 case ISN_STORENR:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002797 case ISN_STOREOUTER:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002798 case ISN_STORE_THIS:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002799 case ISN_STORERANGE:
2800 case ISN_STOREREG:
2801 case ISN_STOREV:
2802 case ISN_STRINDEX:
2803 case ISN_STRSLICE:
2804 case ISN_THROW:
2805 case ISN_TRYCONT:
2806 case ISN_UNLETINDEX:
2807 case ISN_UNLETRANGE:
2808 case ISN_UNPACK:
2809 case ISN_USEDICT:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002810 case ISN_WHILE:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002811 // nothing allocated
2812 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002813 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002814}
2815
2816 void
2817clear_instr_ga(garray_T *gap)
2818{
2819 int idx;
2820
2821 for (idx = 0; idx < gap->ga_len; ++idx)
2822 delete_instr(((isn_T *)gap->ga_data) + idx);
2823 ga_clear(gap);
2824}
2825
2826
2827#endif // defined(FEAT_EVAL)