blob: 3da56bf97d500580774632c0a0b37ae727c1a179 [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.
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200194 * When tostring_flags has TOSTRING_TOLERANT, convert a List to a series of
195 * strings. When tostring_flags has TOSTRING_INTERPOLATE, convert a List or a
196 * Dict to the corresponding textual representation.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000197 */
198 int
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200199may_generate_2STRING(int offset, int tostring_flags, cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000200{
201 isn_T *isn;
202 isntype_T isntype = ISN_2STRING;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000203 type_T *type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000204
205 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +0000206 type = get_type_on_stack(cctx, -1 - offset);
207 switch (type->tt_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000208 {
209 // nothing to be done
210 case VAR_STRING: return OK;
211
212 // conversion possible
213 case VAR_SPECIAL:
214 case VAR_BOOL:
215 case VAR_NUMBER:
216 case VAR_FLOAT:
217 break;
218
219 // conversion possible (with runtime check)
220 case VAR_ANY:
221 case VAR_UNKNOWN:
222 isntype = ISN_2STRING_ANY;
223 break;
224
225 // conversion possible when tolerant
226 case VAR_LIST:
Yegappan Lakshmananf01493c2024-04-14 23:21:02 +0200227 case VAR_DICT:
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200228 if (tostring_flags & TOSTRING_TOLERANT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000229 {
230 isntype = ISN_2STRING_ANY;
231 break;
232 }
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200233 if (tostring_flags & TOSTRING_INTERPOLATE)
234 break;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000235 // FALLTHROUGH
236
237 // conversion not possible
238 case VAR_VOID:
239 case VAR_BLOB:
240 case VAR_FUNC:
241 case VAR_PARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000242 case VAR_JOB:
243 case VAR_CHANNEL:
244 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000245 case VAR_CLASS:
246 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +0200247 case VAR_TYPEALIAS:
Bram Moolenaar078a4612022-01-04 15:17:03 +0000248 to_string_error(type->tt_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000249 return FAIL;
250 }
251
Bram Moolenaar078a4612022-01-04 15:17:03 +0000252 set_type_on_stack(cctx, &t_string, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000253 if ((isn = generate_instr(cctx, isntype)) == NULL)
254 return FAIL;
255 isn->isn_arg.tostring.offset = offset;
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200256 isn->isn_arg.tostring.flags = tostring_flags;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000257
258 return OK;
259}
260
261 static int
Ernie Raele75fde62023-12-21 17:18:54 +0100262check_number_or_float(type_T *typ1, type_T *typ2, char_u *op)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000263{
Ernie Raele75fde62023-12-21 17:18:54 +0100264 vartype_T type1 = typ1->tt_type;
265 vartype_T type2 = typ2->tt_type;
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000266 if (!((type1 == VAR_NUMBER || type1 == VAR_FLOAT
267 || type1 == VAR_ANY || type1 == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000268 && (type2 == VAR_NUMBER || type2 == VAR_FLOAT
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000269 || type2 == VAR_ANY || type2 == VAR_UNKNOWN)))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000270 {
Ernie Raele75fde62023-12-21 17:18:54 +0100271 if (check_type_is_value(typ1) == FAIL
272 || check_type_is_value(typ2) == FAIL)
273 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000274 if (*op == '+')
275 emsg(_(e_wrong_argument_type_for_plus));
276 else
277 semsg(_(e_char_requires_number_or_float_arguments), *op);
278 return FAIL;
279 }
280 return OK;
281}
282
283/*
284 * Generate instruction for "+". For a list this creates a new list.
285 */
286 int
287generate_add_instr(
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000288 cctx_T *cctx,
289 vartype_T vartype,
290 type_T *type1,
291 type_T *type2,
292 exprtype_T expr_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000293{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000294 isn_T *isn = generate_instr_drop(cctx,
295 vartype == VAR_NUMBER ? ISN_OPNR
296 : vartype == VAR_LIST ? ISN_ADDLIST
297 : vartype == VAR_BLOB ? ISN_ADDBLOB
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000298 : vartype == VAR_FLOAT ? ISN_OPFLOAT
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000299 : ISN_OPANY, 1);
300
301 if (vartype != VAR_LIST && vartype != VAR_BLOB
302 && type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000303 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000304 && type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000305 && type2->tt_type != VAR_UNKNOWN
Ernie Raele75fde62023-12-21 17:18:54 +0100306 && check_number_or_float(type1, type2, (char_u *)"+") == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000307 return FAIL;
308
309 if (isn != NULL)
310 {
311 if (isn->isn_type == ISN_ADDLIST)
312 isn->isn_arg.op.op_type = expr_type;
313 else
314 isn->isn_arg.op.op_type = EXPR_ADD;
315 }
316
317 // When concatenating two lists with different member types the member type
318 // becomes "any".
319 if (vartype == VAR_LIST
320 && type1->tt_type == VAR_LIST && type2->tt_type == VAR_LIST
321 && type1->tt_member != type2->tt_member)
Bram Moolenaar078a4612022-01-04 15:17:03 +0000322 set_type_on_stack(cctx, &t_list_any, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000323
324 return isn == NULL ? FAIL : OK;
325}
326
327/*
328 * Get the type to use for an instruction for an operation on "type1" and
329 * "type2". If they are matching use a type-specific instruction. Otherwise
330 * fall back to runtime type checking.
331 */
332 vartype_T
333operator_type(type_T *type1, type_T *type2)
334{
335 if (type1->tt_type == type2->tt_type
336 && (type1->tt_type == VAR_NUMBER
337 || type1->tt_type == VAR_LIST
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000338 || type1->tt_type == VAR_FLOAT
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000339 || type1->tt_type == VAR_BLOB))
340 return type1->tt_type;
341 return VAR_ANY;
342}
343
344/*
345 * Generate an instruction with two arguments. The instruction depends on the
346 * type of the arguments.
347 */
348 int
349generate_two_op(cctx_T *cctx, char_u *op)
350{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000351 type_T *type1;
352 type_T *type2;
353 vartype_T vartype;
354 isn_T *isn;
355
356 RETURN_OK_IF_SKIP(cctx);
357
358 // Get the known type of the two items on the stack.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000359 type1 = get_type_on_stack(cctx, 1);
360 type2 = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000361 vartype = operator_type(type1, type2);
362
363 switch (*op)
364 {
365 case '+':
366 if (generate_add_instr(cctx, vartype, type1, type2,
367 EXPR_COPY) == FAIL)
368 return FAIL;
369 break;
370
371 case '-':
372 case '*':
Ernie Raele75fde62023-12-21 17:18:54 +0100373 case '/': if (check_number_or_float(type1, type2, op) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000374 return FAIL;
375 if (vartype == VAR_NUMBER)
376 isn = generate_instr_drop(cctx, ISN_OPNR, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000377 else if (vartype == VAR_FLOAT)
378 isn = generate_instr_drop(cctx, ISN_OPFLOAT, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000379 else
380 isn = generate_instr_drop(cctx, ISN_OPANY, 1);
381 if (isn != NULL)
382 isn->isn_arg.op.op_type = *op == '*'
383 ? EXPR_MULT : *op == '/'? EXPR_DIV : EXPR_SUB;
384 break;
385
386 case '%': if ((type1->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000387 && type1->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000388 && type1->tt_type != VAR_NUMBER)
389 || (type2->tt_type != VAR_ANY
Bram Moolenaar59618fe2021-12-21 12:32:17 +0000390 && type2->tt_type != VAR_UNKNOWN
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000391 && type2->tt_type != VAR_NUMBER))
392 {
393 emsg(_(e_percent_requires_number_arguments));
394 return FAIL;
395 }
396 isn = generate_instr_drop(cctx,
397 vartype == VAR_NUMBER ? ISN_OPNR : ISN_OPANY, 1);
398 if (isn != NULL)
399 isn->isn_arg.op.op_type = EXPR_REM;
400 break;
401 }
402
403 // correct type of result
404 if (vartype == VAR_ANY)
405 {
406 type_T *type = &t_any;
407
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000408 // float+number and number+float results in float
409 if ((type1->tt_type == VAR_NUMBER || type1->tt_type == VAR_FLOAT)
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100410 && (type2->tt_type == VAR_NUMBER || type2->tt_type == VAR_FLOAT))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000411 type = &t_float;
Bram Moolenaar078a4612022-01-04 15:17:03 +0000412 set_type_on_stack(cctx, type, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000413 }
414
415 return OK;
416}
417
418/*
Ernie Raele75fde62023-12-21 17:18:54 +0100419 * Choose correct error message for the specified type information.
420 */
421 static isntype_T
422compare_isn_not_values(typval_T *tv, type_T *type)
423{
424 if (tv != NULL)
Christian Brabandt6b9492e2023-12-24 11:14:37 +0100425 (void)check_typval_is_value(tv);
Ernie Raele75fde62023-12-21 17:18:54 +0100426 else
Christian Brabandt6b9492e2023-12-24 11:14:37 +0100427 (void)check_type_is_value(type);
Ernie Raele75fde62023-12-21 17:18:54 +0100428 return ISN_DROP;
429}
430
431/*
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000432 * Get the instruction to use for comparing two values with specified types.
433 * Either "tv1" and "tv2" are passed or "type1" and "type2".
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000434 * Return ISN_DROP when failed.
435 */
436 static isntype_T
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000437get_compare_isn(
Bram Moolenaar2ed57ac2023-04-01 22:05:38 +0100438 exprtype_T exprtype,
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000439 typval_T *tv1,
440 typval_T *tv2,
441 type_T *type1,
442 type_T *type2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000443{
444 isntype_T isntype = ISN_DROP;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000445 vartype_T vartype1 = tv1 != NULL ? tv1->v_type : type1->tt_type;
446 vartype_T vartype2 = tv2 != NULL ? tv2->v_type : type2->tt_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000447
Ernie Raele75fde62023-12-21 17:18:54 +0100448 if (vartype1 == VAR_CLASS || vartype1 == VAR_TYPEALIAS)
449 return compare_isn_not_values(tv1, type1);
450 if (vartype2 == VAR_CLASS || vartype2 == VAR_TYPEALIAS)
451 return compare_isn_not_values(tv2, type2);
452
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000453 if (vartype1 == vartype2)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000454 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000455 switch (vartype1)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000456 {
457 case VAR_BOOL: isntype = ISN_COMPAREBOOL; break;
458 case VAR_SPECIAL: isntype = ISN_COMPARESPECIAL; break;
459 case VAR_NUMBER: isntype = ISN_COMPARENR; break;
460 case VAR_FLOAT: isntype = ISN_COMPAREFLOAT; break;
461 case VAR_STRING: isntype = ISN_COMPARESTRING; break;
462 case VAR_BLOB: isntype = ISN_COMPAREBLOB; break;
463 case VAR_LIST: isntype = ISN_COMPARELIST; break;
464 case VAR_DICT: isntype = ISN_COMPAREDICT; break;
465 case VAR_FUNC: isntype = ISN_COMPAREFUNC; break;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000466 case VAR_OBJECT: isntype = ISN_COMPAREOBJECT; break;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000467 default: isntype = ISN_COMPAREANY; break;
468 }
469 }
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000470 else if (vartype1 == VAR_ANY || vartype2 == VAR_ANY
471 || ((vartype1 == VAR_NUMBER || vartype1 == VAR_FLOAT)
472 && (vartype2 == VAR_NUMBER || vartype2 == VAR_FLOAT))
473 || (vartype1 == VAR_FUNC && vartype2 == VAR_PARTIAL)
474 || (vartype1 == VAR_PARTIAL && vartype2 == VAR_FUNC))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000475 isntype = ISN_COMPAREANY;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000476 else if (vartype1 == VAR_SPECIAL || vartype2 == VAR_SPECIAL)
Bram Moolenaar7a222242022-03-01 19:23:24 +0000477 {
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000478 if ((vartype1 == VAR_SPECIAL
479 && (tv1 != NULL ? tv1->vval.v_number == VVAL_NONE
480 : type1 == &t_none)
481 && vartype2 != VAR_STRING)
482 || (vartype2 == VAR_SPECIAL
483 && (tv2 != NULL ? tv2->vval.v_number == VVAL_NONE
484 : type2 == &t_none)
485 && vartype1 != VAR_STRING))
486 {
487 semsg(_(e_cannot_compare_str_with_str),
488 vartype_name(vartype1), vartype_name(vartype2));
489 return ISN_DROP;
490 }
Bram Moolenaar05667812022-03-15 20:21:33 +0000491 // although comparing null with number, float or bool is not useful, we
492 // allow it
Bram Moolenaar7a222242022-03-01 19:23:24 +0000493 isntype = ISN_COMPARENULL;
494 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000495
496 if ((exprtype == EXPR_IS || exprtype == EXPR_ISNOT)
497 && (isntype == ISN_COMPAREBOOL
498 || isntype == ISN_COMPARESPECIAL
499 || isntype == ISN_COMPARENR
500 || isntype == ISN_COMPAREFLOAT))
501 {
502 semsg(_(e_cannot_use_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000503 exprtype == EXPR_IS ? "is" : "isnot" , vartype_name(vartype1));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000504 return ISN_DROP;
505 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000506 if (!(exprtype == EXPR_IS || exprtype == EXPR_ISNOT
507 || exprtype == EXPR_EQUAL || exprtype == EXPR_NEQUAL)
Ernie Raele75fde62023-12-21 17:18:54 +0100508 && (isntype == ISN_COMPAREOBJECT))
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +0000509 {
510 semsg(_(e_invalid_operation_for_str), vartype_name(vartype1));
511 return ISN_DROP;
512 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000513 if (isntype == ISN_DROP
Bram Moolenaar2ed57ac2023-04-01 22:05:38 +0100514 || (isntype != ISN_COMPARENULL
515 && (((exprtype != EXPR_EQUAL
516 && exprtype != EXPR_NEQUAL
517 && (vartype1 == VAR_BOOL || vartype1 == VAR_SPECIAL
518 || vartype2 == VAR_BOOL || vartype2 == VAR_SPECIAL)))
519 || ((exprtype != EXPR_EQUAL
520 && exprtype != EXPR_NEQUAL
521 && exprtype != EXPR_IS
522 && exprtype != EXPR_ISNOT
523 && (vartype1 == VAR_BLOB || vartype2 == VAR_BLOB
524 || vartype1 == VAR_LIST || vartype2 == VAR_LIST))))))
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000525 {
526 semsg(_(e_cannot_compare_str_with_str),
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000527 vartype_name(vartype1), vartype_name(vartype2));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000528 return ISN_DROP;
529 }
530 return isntype;
531}
532
533 int
534check_compare_types(exprtype_T type, typval_T *tv1, typval_T *tv2)
535{
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000536 if (get_compare_isn(type, tv1, tv2, NULL, NULL) == ISN_DROP)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000537 return FAIL;
538 return OK;
539}
540
541/*
542 * Generate an ISN_COMPARE* instruction with a boolean result.
543 */
544 int
545generate_COMPARE(cctx_T *cctx, exprtype_T exprtype, int ic)
546{
547 isntype_T isntype;
548 isn_T *isn;
549 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000550
551 RETURN_OK_IF_SKIP(cctx);
552
553 // Get the known type of the two items on the stack. If they are matching
554 // use a type-specific instruction. Otherwise fall back to runtime type
555 // checking.
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000556 isntype = get_compare_isn(exprtype, NULL, NULL,
557 get_type_on_stack(cctx, 1), get_type_on_stack(cctx, 0));
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000558 if (isntype == ISN_DROP)
559 return FAIL;
560
561 if ((isn = generate_instr(cctx, isntype)) == NULL)
562 return FAIL;
563 isn->isn_arg.op.op_type = exprtype;
564 isn->isn_arg.op.op_ic = ic;
565
566 // takes two arguments, puts one bool back
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100567 --stack->ga_len;
568 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000569
570 return OK;
571}
572
573/*
LemonBoy372bcce2022-04-25 12:43:20 +0100574 * Generate an ISN_CONCAT instruction.
575 * "count" is the number of stack elements to join together and it must be
576 * greater or equal to one.
577 * The caller ensures all the "count" elements on the stack have the right type.
578 */
579 int
580generate_CONCAT(cctx_T *cctx, int count)
581{
582 isn_T *isn;
583 garray_T *stack = &cctx->ctx_type_stack;
584
585 RETURN_OK_IF_SKIP(cctx);
586
LemonBoy372bcce2022-04-25 12:43:20 +0100587 if ((isn = generate_instr(cctx, ISN_CONCAT)) == NULL)
588 return FAIL;
589 isn->isn_arg.number = count;
590
591 // drop the argument types
592 stack->ga_len -= count - 1;
593
594 return OK;
595}
596
597/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000598 * Generate an ISN_2BOOL instruction.
599 * "offset" is the offset in the type stack.
600 */
601 int
602generate_2BOOL(cctx_T *cctx, int invert, int offset)
603{
604 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000605
606 RETURN_OK_IF_SKIP(cctx);
607 if ((isn = generate_instr(cctx, ISN_2BOOL)) == NULL)
608 return FAIL;
609 isn->isn_arg.tobool.invert = invert;
610 isn->isn_arg.tobool.offset = offset;
611
612 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000613 set_type_on_stack(cctx, &t_bool, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000614
615 return OK;
616}
617
618/*
619 * Generate an ISN_COND2BOOL instruction.
620 */
621 int
622generate_COND2BOOL(cctx_T *cctx)
623{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000624 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100625 if (generate_instr(cctx, ISN_COND2BOOL) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000626 return FAIL;
627
628 // type becomes bool
Bram Moolenaar078a4612022-01-04 15:17:03 +0000629 set_type_on_stack(cctx, &t_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000630
631 return OK;
632}
633
634 int
635generate_TYPECHECK(
636 cctx_T *cctx,
637 type_T *expected,
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000638 int number_ok, // add TTFLAG_NUMBER_OK flag
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000639 int offset,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100640 int is_var,
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000641 int argidx)
642{
643 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000644
645 RETURN_OK_IF_SKIP(cctx);
646 if ((isn = generate_instr(cctx, ISN_CHECKTYPE)) == NULL)
647 return FAIL;
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000648 type_T *tt;
649 if (expected->tt_type == VAR_FLOAT && number_ok)
650 {
651 // always allocate, also for static types
652 tt = ALLOC_ONE(type_T);
653 if (tt != NULL)
654 {
655 *tt = *expected;
Bram Moolenaarc4b3f642022-12-30 10:36:34 +0000656 tt->tt_flags &= ~TTFLAG_STATIC;
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000657 tt->tt_flags |= TTFLAG_NUMBER_OK;
658 }
659 }
660 else
661 tt = alloc_type(expected);
662
663 isn->isn_arg.type.ct_type = tt;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000664 isn->isn_arg.type.ct_off = (int8_T)offset;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +0100665 isn->isn_arg.type.ct_is_var = is_var;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000666 isn->isn_arg.type.ct_arg_idx = (int8_T)argidx;
667
668 // type becomes expected
Bram Moolenaar078a4612022-01-04 15:17:03 +0000669 set_type_on_stack(cctx, expected, -1 - offset);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000670
671 return OK;
672}
673
674 int
675generate_SETTYPE(
676 cctx_T *cctx,
677 type_T *expected)
678{
679 isn_T *isn;
680
681 RETURN_OK_IF_SKIP(cctx);
682 if ((isn = generate_instr(cctx, ISN_SETTYPE)) == NULL)
683 return FAIL;
684 isn->isn_arg.type.ct_type = alloc_type(expected);
685 return OK;
686}
687
688/*
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000689 * Generate an ISN_PUSHOBJ instruction. Object is always NULL.
690 */
Ernie Rael5c018be2023-08-27 18:40:26 +0200691 int
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000692generate_PUSHOBJ(cctx_T *cctx)
693{
694 RETURN_OK_IF_SKIP(cctx);
Ernie Rael5c018be2023-08-27 18:40:26 +0200695 if (generate_instr_type(cctx, ISN_PUSHOBJ, &t_object) == NULL)
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000696 return FAIL;
697 return OK;
698}
699
700/*
701 * Generate an ISN_PUSHCLASS instruction. "class" can be NULL.
702 */
703 static int
704generate_PUSHCLASS(cctx_T *cctx, class_T *class)
705{
706 RETURN_OK_IF_SKIP(cctx);
707 isn_T *isn = generate_instr_type(cctx, ISN_PUSHCLASS,
708 class == NULL ? &t_any : &class->class_type);
709 if (isn == NULL)
710 return FAIL;
Bram Moolenaar30a84472023-02-27 08:07:14 +0000711 isn->isn_arg.classarg = class;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000712 if (class != NULL)
713 ++class->class_refcount;
714 return OK;
715}
716
717/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000718 * Generate a PUSH instruction for "tv".
719 * "tv" will be consumed or cleared.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000720 */
721 int
722generate_tv_PUSH(cctx_T *cctx, typval_T *tv)
723{
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100724 switch (tv->v_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000725 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100726 case VAR_BOOL:
727 generate_PUSHBOOL(cctx, tv->vval.v_number);
728 break;
729 case VAR_SPECIAL:
730 generate_PUSHSPEC(cctx, tv->vval.v_number);
731 break;
732 case VAR_NUMBER:
733 generate_PUSHNR(cctx, tv->vval.v_number);
734 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100735 case VAR_FLOAT:
736 generate_PUSHF(cctx, tv->vval.v_float);
737 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100738 case VAR_BLOB:
739 generate_PUSHBLOB(cctx, tv->vval.v_blob);
740 tv->vval.v_blob = NULL;
741 break;
742 case VAR_LIST:
743 if (tv->vval.v_list != NULL)
744 iemsg("non-empty list constant not supported");
745 generate_NEWLIST(cctx, 0, TRUE);
746 break;
747 case VAR_DICT:
748 if (tv->vval.v_dict != NULL)
749 iemsg("non-empty dict constant not supported");
750 generate_NEWDICT(cctx, 0, TRUE);
751 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000752#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100753 case VAR_JOB:
754 if (tv->vval.v_job != NULL)
755 iemsg("non-null job constant not supported");
756 generate_PUSHJOB(cctx);
757 break;
758 case VAR_CHANNEL:
759 if (tv->vval.v_channel != NULL)
760 iemsg("non-null channel constant not supported");
761 generate_PUSHCHANNEL(cctx);
762 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000763#endif
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100764 case VAR_FUNC:
765 if (tv->vval.v_string != NULL)
766 iemsg("non-null function constant not supported");
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100767 generate_PUSHFUNC(cctx, NULL, &t_func_unknown, TRUE);
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100768 break;
769 case VAR_PARTIAL:
770 if (tv->vval.v_partial != NULL)
771 iemsg("non-null partial constant not supported");
772 if (generate_instr_type(cctx, ISN_NEWPARTIAL, &t_func_unknown)
773 == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000774 return FAIL;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100775 break;
776 case VAR_STRING:
777 generate_PUSHS(cctx, &tv->vval.v_string);
778 tv->vval.v_string = NULL;
779 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +0000780 case VAR_OBJECT:
781 if (tv->vval.v_object != NULL)
782 {
783 emsg(_(e_cannot_use_non_null_object));
784 return FAIL;
785 }
786 generate_PUSHOBJ(cctx);
787 break;
788 case VAR_CLASS:
789 generate_PUSHCLASS(cctx, tv->vval.v_class);
790 break;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100791 default:
792 siemsg("constant type %d not supported", tv->v_type);
793 clear_tv(tv);
794 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000795 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100796 tv->v_type = VAR_UNKNOWN;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000797 return OK;
798}
799
800/*
801 * Generate an ISN_PUSHNR instruction.
802 */
803 int
804generate_PUSHNR(cctx_T *cctx, varnumber_T number)
805{
806 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000807
808 RETURN_OK_IF_SKIP(cctx);
809 if ((isn = generate_instr_type(cctx, ISN_PUSHNR, &t_number)) == NULL)
810 return FAIL;
811 isn->isn_arg.number = number;
812
813 if (number == 0 || number == 1)
814 // A 0 or 1 number can also be used as a bool.
Bram Moolenaar078a4612022-01-04 15:17:03 +0000815 set_type_on_stack(cctx, &t_number_bool, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000816 return OK;
817}
818
819/*
820 * Generate an ISN_PUSHBOOL instruction.
821 */
822 int
823generate_PUSHBOOL(cctx_T *cctx, varnumber_T number)
824{
825 isn_T *isn;
826
827 RETURN_OK_IF_SKIP(cctx);
828 if ((isn = generate_instr_type(cctx, ISN_PUSHBOOL, &t_bool)) == NULL)
829 return FAIL;
830 isn->isn_arg.number = number;
831
832 return OK;
833}
834
835/*
836 * Generate an ISN_PUSHSPEC instruction.
837 */
838 int
839generate_PUSHSPEC(cctx_T *cctx, varnumber_T number)
840{
841 isn_T *isn;
842
843 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +0000844 if ((isn = generate_instr_type(cctx, ISN_PUSHSPEC,
845 number == VVAL_NULL ? &t_null : &t_none)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000846 return FAIL;
847 isn->isn_arg.number = number;
848
849 return OK;
850}
851
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000852/*
853 * Generate an ISN_PUSHF instruction.
854 */
855 int
856generate_PUSHF(cctx_T *cctx, float_T fnumber)
857{
858 isn_T *isn;
859
860 RETURN_OK_IF_SKIP(cctx);
861 if ((isn = generate_instr_type(cctx, ISN_PUSHF, &t_float)) == NULL)
862 return FAIL;
863 isn->isn_arg.fnumber = fnumber;
864
865 return OK;
866}
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000867
868/*
869 * Generate an ISN_PUSHS instruction.
870 * Consumes "*str". When freed *str is set to NULL, unless "str" is NULL.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100871 * Note that if "str" is used in the instruction OK is returned and "*str" is
872 * not set to NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000873 */
874 int
875generate_PUSHS(cctx_T *cctx, char_u **str)
876{
877 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100878 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000879
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100880 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000881 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100882 if ((isn = generate_instr_type(cctx, ISN_PUSHS, &t_string)) == NULL)
883 ret = FAIL;
884 else
885 {
886 isn->isn_arg.string = str == NULL ? NULL : *str;
887 return OK;
888 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000889 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +0100890 if (str != NULL)
891 VIM_CLEAR(*str);
892 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000893}
894
895/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000896 * Generate an ISN_PUSHCHANNEL instruction. Channel is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000897 */
898 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000899generate_PUSHCHANNEL(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000900{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000901 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100902#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100903 if (generate_instr_type(cctx, ISN_PUSHCHANNEL, &t_channel) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000904 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000905 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100906#else
907 emsg(_(e_channel_job_feature_not_available));
908 return FAIL;
909#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000910}
911
912/*
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000913 * Generate an ISN_PUSHJOB instruction. Job is always NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000914 */
915 int
Bram Moolenaar397a87a2022-03-20 21:14:15 +0000916generate_PUSHJOB(cctx_T *cctx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000917{
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000918 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100919#ifdef FEAT_JOB_CHANNEL
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +0100920 if (generate_instr_type(cctx, ISN_PUSHJOB, &t_job) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000921 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000922 return OK;
Bram Moolenaarc9af6172022-05-04 16:46:54 +0100923#else
924 emsg(_(e_channel_job_feature_not_available));
925 return FAIL;
926#endif
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000927}
928
929/*
930 * Generate an ISN_PUSHBLOB instruction.
931 * Consumes "blob".
932 */
933 int
934generate_PUSHBLOB(cctx_T *cctx, blob_T *blob)
935{
936 isn_T *isn;
937
938 RETURN_OK_IF_SKIP(cctx);
939 if ((isn = generate_instr_type(cctx, ISN_PUSHBLOB, &t_blob)) == NULL)
940 return FAIL;
941 isn->isn_arg.blob = blob;
942
943 return OK;
944}
945
946/*
947 * Generate an ISN_PUSHFUNC instruction with name "name".
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100948 * When "may_prefix" is TRUE prefix "g:" unless "name" is script-local or
949 * autoload.
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000950 */
951 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100952generate_PUSHFUNC(cctx_T *cctx, char_u *name, type_T *type, int may_prefix)
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000953{
954 isn_T *isn;
955 char_u *funcname;
956
957 RETURN_OK_IF_SKIP(cctx);
958 if ((isn = generate_instr_type(cctx, ISN_PUSHFUNC, type)) == NULL)
959 return FAIL;
960 if (name == NULL)
961 funcname = NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100962 else if (!may_prefix
963 || *name == K_SPECIAL // script-local
Bram Moolenaard041f422022-01-12 19:54:00 +0000964 || vim_strchr(name, AUTOLOAD_CHAR) != NULL) // autoload
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000965 funcname = vim_strsave(name);
966 else
967 {
968 funcname = alloc(STRLEN(name) + 3);
969 if (funcname != NULL)
970 {
971 STRCPY(funcname, "g:");
972 STRCPY(funcname + 2, name);
973 }
974 }
975
976 isn->isn_arg.string = funcname;
977 return OK;
978}
979
980/*
Bram Moolenaar06b77222022-01-25 15:51:56 +0000981 * Generate an ISN_AUTOLOAD instruction.
982 */
983 int
984generate_AUTOLOAD(cctx_T *cctx, char_u *name, type_T *type)
985{
986 isn_T *isn;
987
988 RETURN_OK_IF_SKIP(cctx);
989 if ((isn = generate_instr_type(cctx, ISN_AUTOLOAD, type)) == NULL)
990 return FAIL;
991 isn->isn_arg.string = vim_strsave(name);
992 if (isn->isn_arg.string == NULL)
993 return FAIL;
994 return OK;
995}
996
997/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000998 * Generate an ISN_GETITEM instruction with "index".
999 * "with_op" is TRUE for "+=" and other operators, the stack has the current
1000 * value below the list with values.
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001001 * Caller must check the type is a list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001002 */
1003 int
1004generate_GETITEM(cctx_T *cctx, int index, int with_op)
1005{
1006 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001007 type_T *type = get_type_on_stack(cctx, with_op ? 1 : 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001008 type_T *item_type = &t_any;
1009
1010 RETURN_OK_IF_SKIP(cctx);
1011
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001012 item_type = type->tt_member;
1013 if ((isn = generate_instr(cctx, ISN_GETITEM)) == NULL)
1014 return FAIL;
1015 isn->isn_arg.getitem.gi_index = index;
1016 isn->isn_arg.getitem.gi_with_op = with_op;
1017
1018 // add the item type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001019 return push_type_stack(cctx, item_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001020}
1021
1022/*
1023 * Generate an ISN_SLICE instruction with "count".
1024 */
1025 int
1026generate_SLICE(cctx_T *cctx, int count)
1027{
1028 isn_T *isn;
1029
1030 RETURN_OK_IF_SKIP(cctx);
1031 if ((isn = generate_instr(cctx, ISN_SLICE)) == NULL)
1032 return FAIL;
1033 isn->isn_arg.number = count;
1034 return OK;
1035}
1036
1037/*
1038 * Generate an ISN_CHECKLEN instruction with "min_len".
1039 */
1040 int
1041generate_CHECKLEN(cctx_T *cctx, int min_len, int more_OK)
1042{
1043 isn_T *isn;
1044
1045 RETURN_OK_IF_SKIP(cctx);
1046
1047 if ((isn = generate_instr(cctx, ISN_CHECKLEN)) == NULL)
1048 return FAIL;
1049 isn->isn_arg.checklen.cl_min_len = min_len;
1050 isn->isn_arg.checklen.cl_more_OK = more_OK;
1051
1052 return OK;
1053}
1054
1055/*
1056 * Generate an ISN_STORE instruction.
1057 */
1058 int
1059generate_STORE(cctx_T *cctx, isntype_T isn_type, int idx, char_u *name)
1060{
1061 isn_T *isn;
1062
1063 RETURN_OK_IF_SKIP(cctx);
1064 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1065 return FAIL;
1066 if (name != NULL)
1067 isn->isn_arg.string = vim_strsave(name);
1068 else
1069 isn->isn_arg.number = idx;
1070
1071 return OK;
1072}
1073
1074/*
Bram Moolenaard505d172022-12-18 21:42:55 +00001075 * Generate an ISN_LOAD_CLASSMEMBER ("load" == TRUE) or ISN_STORE_CLASSMEMBER
1076 * ("load" == FALSE) instruction.
1077 */
1078 int
1079generate_CLASSMEMBER(
1080 cctx_T *cctx,
1081 int load,
1082 class_T *cl,
1083 int idx)
1084{
1085 isn_T *isn;
1086
1087 RETURN_OK_IF_SKIP(cctx);
1088 if (load)
1089 {
1090 ocmember_T *m = &cl->class_class_members[idx];
1091 isn = generate_instr_type(cctx, ISN_LOAD_CLASSMEMBER, m->ocm_type);
1092 }
1093 else
1094 {
1095 isn = generate_instr_drop(cctx, ISN_STORE_CLASSMEMBER, 1);
1096 }
1097 if (isn == NULL)
1098 return FAIL;
1099 isn->isn_arg.classmember.cm_class = cl;
1100 ++cl->class_refcount;
1101 isn->isn_arg.classmember.cm_idx = idx;
1102
1103 return OK;
1104}
1105
1106/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001107 * Generate an ISN_STOREOUTER instruction.
1108 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +00001109 static int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001110generate_STOREOUTER(cctx_T *cctx, int idx, int level, int loop_idx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001111{
1112 isn_T *isn;
1113
1114 RETURN_OK_IF_SKIP(cctx);
1115 if ((isn = generate_instr_drop(cctx, ISN_STOREOUTER, 1)) == NULL)
1116 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001117 if (level == 1 && loop_idx >= 0 && idx >= loop_idx)
1118 {
1119 // Store a variable defined in a loop. A copy will be made at the end
1120 // of the loop. TODO: how about deeper nesting?
1121 isn->isn_arg.outer.outer_idx = idx - loop_idx;
1122 isn->isn_arg.outer.outer_depth = OUTER_LOOP_DEPTH;
1123 }
1124 else
1125 {
1126 isn->isn_arg.outer.outer_idx = idx;
1127 isn->isn_arg.outer.outer_depth = level;
1128 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001129
1130 return OK;
1131}
1132
1133/*
1134 * Generate an ISN_STORENR instruction (short for ISN_PUSHNR + ISN_STORE)
1135 */
1136 int
1137generate_STORENR(cctx_T *cctx, int idx, varnumber_T value)
1138{
1139 isn_T *isn;
1140
1141 RETURN_OK_IF_SKIP(cctx);
1142 if ((isn = generate_instr(cctx, ISN_STORENR)) == NULL)
1143 return FAIL;
1144 isn->isn_arg.storenr.stnr_idx = idx;
1145 isn->isn_arg.storenr.stnr_val = value;
1146
1147 return OK;
1148}
1149
1150/*
1151 * Generate an ISN_STOREOPT or ISN_STOREFUNCOPT instruction
1152 */
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +00001153 static int
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001154generate_STOREOPT(
1155 cctx_T *cctx,
1156 isntype_T isn_type,
1157 char_u *name,
1158 int opt_flags)
1159{
1160 isn_T *isn;
1161
1162 RETURN_OK_IF_SKIP(cctx);
1163 if ((isn = generate_instr_drop(cctx, isn_type, 1)) == NULL)
1164 return FAIL;
1165 isn->isn_arg.storeopt.so_name = vim_strsave(name);
1166 isn->isn_arg.storeopt.so_flags = opt_flags;
1167
1168 return OK;
1169}
1170
1171/*
1172 * Generate an ISN_LOAD or similar instruction.
1173 */
1174 int
1175generate_LOAD(
1176 cctx_T *cctx,
1177 isntype_T isn_type,
1178 int idx,
1179 char_u *name,
1180 type_T *type)
1181{
1182 isn_T *isn;
1183
1184 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001185 if ((isn = generate_instr_type2(cctx, isn_type, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001186 return FAIL;
1187 if (name != NULL)
1188 isn->isn_arg.string = vim_strsave(name);
1189 else
1190 isn->isn_arg.number = idx;
1191
1192 return OK;
1193}
1194
1195/*
1196 * Generate an ISN_LOADOUTER instruction
1197 */
1198 int
1199generate_LOADOUTER(
1200 cctx_T *cctx,
1201 int idx,
1202 int nesting,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001203 int loop_depth,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001204 int loop_idx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001205 type_T *type)
1206{
1207 isn_T *isn;
1208
1209 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar078a4612022-01-04 15:17:03 +00001210 if ((isn = generate_instr_type2(cctx, ISN_LOADOUTER, type, type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001211 return FAIL;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001212 if (nesting == 1 && loop_idx >= 0 && idx >= loop_idx)
1213 {
1214 // Load a variable defined in a loop. A copy will be made at the end
Bram Moolenaarcc341812022-09-19 15:54:34 +01001215 // of the loop.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001216 isn->isn_arg.outer.outer_idx = idx - loop_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001217 isn->isn_arg.outer.outer_depth = -loop_depth - 1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001218 }
1219 else
1220 {
1221 isn->isn_arg.outer.outer_idx = idx;
1222 isn->isn_arg.outer.outer_depth = nesting;
1223 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001224
1225 return OK;
1226}
1227
1228/*
1229 * Generate an ISN_LOADV instruction for v:var.
1230 */
1231 int
1232generate_LOADV(
1233 cctx_T *cctx,
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001234 char_u *name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001235{
1236 int di_flags;
1237 int vidx = find_vim_var(name, &di_flags);
1238 type_T *type;
1239
1240 RETURN_OK_IF_SKIP(cctx);
1241 if (vidx < 0)
1242 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001243 semsg(_(e_variable_not_found_str), name);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001244 return FAIL;
1245 }
Bram Moolenaard787e402021-12-24 21:36:12 +00001246 type = get_vim_var_type(vidx, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001247 return generate_LOAD(cctx, ISN_LOADV, vidx, NULL, type);
1248}
1249
1250/*
1251 * Generate an ISN_UNLET instruction.
1252 */
1253 int
1254generate_UNLET(cctx_T *cctx, isntype_T isn_type, char_u *name, int forceit)
1255{
1256 isn_T *isn;
1257
1258 RETURN_OK_IF_SKIP(cctx);
1259 if ((isn = generate_instr(cctx, isn_type)) == NULL)
1260 return FAIL;
1261 isn->isn_arg.unlet.ul_name = vim_strsave(name);
1262 isn->isn_arg.unlet.ul_forceit = forceit;
1263
1264 return OK;
1265}
1266
1267/*
1268 * Generate an ISN_LOCKCONST instruction.
1269 */
1270 int
1271generate_LOCKCONST(cctx_T *cctx)
1272{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001273 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmanan6b085b92022-09-04 12:47:21 +01001274 if (generate_instr(cctx, ISN_LOCKCONST) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001275 return FAIL;
1276 return OK;
1277}
1278
1279/*
1280 * Generate an ISN_LOADS instruction.
1281 */
1282 int
1283generate_OLDSCRIPT(
1284 cctx_T *cctx,
1285 isntype_T isn_type,
1286 char_u *name,
1287 int sid,
1288 type_T *type)
1289{
1290 isn_T *isn;
1291
1292 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001293 if (isn_type == ISN_LOADS || isn_type == ISN_LOADEXPORT)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001294 isn = generate_instr_type(cctx, isn_type, type);
1295 else
1296 isn = generate_instr_drop(cctx, isn_type, 1);
1297 if (isn == NULL)
1298 return FAIL;
1299 isn->isn_arg.loadstore.ls_name = vim_strsave(name);
1300 isn->isn_arg.loadstore.ls_sid = sid;
1301
1302 return OK;
1303}
1304
1305/*
1306 * Generate an ISN_LOADSCRIPT or ISN_STORESCRIPT instruction.
1307 */
1308 int
1309generate_VIM9SCRIPT(
1310 cctx_T *cctx,
1311 isntype_T isn_type,
1312 int sid,
1313 int idx,
1314 type_T *type)
1315{
1316 isn_T *isn;
1317 scriptref_T *sref;
1318 scriptitem_T *si = SCRIPT_ITEM(sid);
1319
1320 RETURN_OK_IF_SKIP(cctx);
1321 if (isn_type == ISN_LOADSCRIPT)
Bram Moolenaar160afdb2022-02-06 17:17:02 +00001322 isn = generate_instr_type2(cctx, isn_type, type, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001323 else
1324 isn = generate_instr_drop(cctx, isn_type, 1);
1325 if (isn == NULL)
1326 return FAIL;
1327
1328 // This requires three arguments, which doesn't fit in an instruction, thus
1329 // we need to allocate a struct for this.
1330 sref = ALLOC_ONE(scriptref_T);
1331 if (sref == NULL)
1332 return FAIL;
1333 isn->isn_arg.script.scriptref = sref;
1334 sref->sref_sid = sid;
1335 sref->sref_idx = idx;
1336 sref->sref_seq = si->sn_script_seq;
1337 sref->sref_type = type;
1338 return OK;
1339}
1340
1341/*
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001342 * Generate an ISN_NEWLIST instruction for "count" items.
1343 * "use_null" is TRUE for null_list.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001344 */
1345 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001346generate_NEWLIST(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001347{
1348 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001349 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001350 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001351 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001352
1353 RETURN_OK_IF_SKIP(cctx);
1354 if ((isn = generate_instr(cctx, ISN_NEWLIST)) == NULL)
1355 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001356 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001357
Bram Moolenaar078a4612022-01-04 15:17:03 +00001358 // Get the member type and the declared member type from all the items on
1359 // the stack.
Ernie Raelfa831102023-12-14 20:06:39 +01001360 if ((member_type = get_member_type_from_stack(count, 1, cctx)) == NULL)
1361 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001362 type = get_list_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001363 decl_type = get_list_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001364
1365 // drop the value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001366 cctx->ctx_type_stack.ga_len -= count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001367
1368 // add the list type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001369 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001370}
1371
1372/*
1373 * Generate an ISN_NEWDICT instruction.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001374 * "use_null" is TRUE for null_dict.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001375 */
1376 int
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001377generate_NEWDICT(cctx_T *cctx, int count, int use_null)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001378{
1379 isn_T *isn;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001380 type_T *member_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001381 type_T *type;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001382 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001383
1384 RETURN_OK_IF_SKIP(cctx);
1385 if ((isn = generate_instr(cctx, ISN_NEWDICT)) == NULL)
1386 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01001387 isn->isn_arg.number = use_null ? -1 : count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001388
Ernie Raelfa831102023-12-14 20:06:39 +01001389 if ((member_type = get_member_type_from_stack(count, 2, cctx)) == NULL)
1390 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001391 type = get_dict_type(member_type, cctx->ctx_type_list);
Bram Moolenaar2626d6a2022-02-06 15:49:35 +00001392 decl_type = get_dict_type(&t_any, cctx->ctx_type_list);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001393
1394 // drop the key and value types
Bram Moolenaar078a4612022-01-04 15:17:03 +00001395 cctx->ctx_type_stack.ga_len -= 2 * count;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001396
1397 // add the dict type to the type stack
Bram Moolenaar078a4612022-01-04 15:17:03 +00001398 return push_type_stack2(cctx, type, decl_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001399}
1400
1401/*
1402 * Generate an ISN_FUNCREF instruction.
Bram Moolenaar313e4722023-02-08 20:55:27 +00001403 * For "obj.Method" "cl" is the class of the object (can be an interface or a
1404 * base class) and "fi" the index of the method on that class.
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001405 * "isn_idx" is set to the index of the instruction, so that fr_dfunc_idx can
1406 * be set later. The index is used instead of a pointer to the instruction
1407 * because the instruction memory can be reallocated.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001408 */
1409 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001410generate_FUNCREF(
1411 cctx_T *cctx,
1412 ufunc_T *ufunc,
Bram Moolenaar313e4722023-02-08 20:55:27 +00001413 class_T *cl,
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001414 int object_method,
Bram Moolenaar313e4722023-02-08 20:55:27 +00001415 int fi,
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001416 int *isn_idx)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001417{
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001418 isn_T *isn;
1419 type_T *type;
1420 funcref_extra_T *extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001421 loopvarinfo_T loopinfo;
1422 int has_vars;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001423
1424 RETURN_OK_IF_SKIP(cctx);
1425 if ((isn = generate_instr(cctx, ISN_FUNCREF)) == NULL)
1426 return FAIL;
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001427 if (isn_idx != NULL)
1428 // save the index of the new instruction
1429 *isn_idx = cctx->ctx_instr.ga_len - 1;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001430
Bram Moolenaarcc341812022-09-19 15:54:34 +01001431 has_vars = get_loop_var_info(cctx, &loopinfo);
Bram Moolenaar313e4722023-02-08 20:55:27 +00001432 if (ufunc->uf_def_status == UF_NOT_COMPILED || has_vars || cl != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001433 {
1434 extra = ALLOC_CLEAR_ONE(funcref_extra_T);
1435 if (extra == NULL)
1436 return FAIL;
1437 isn->isn_arg.funcref.fr_extra = extra;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001438 extra->fre_loopvar_info = loopinfo;
Bram Moolenaar313e4722023-02-08 20:55:27 +00001439 if (cl != NULL)
1440 {
1441 extra->fre_class = cl;
1442 ++cl->class_refcount;
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001443 extra->fre_object_method = object_method;
Bram Moolenaar313e4722023-02-08 20:55:27 +00001444 extra->fre_method_idx = fi;
1445 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001446 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00001447 if (ufunc->uf_def_status == UF_NOT_COMPILED || cl != NULL)
John Marriottb32800f2025-02-01 15:25:34 +01001448 extra->fre_func_name = vim_strnsave(ufunc->uf_name, ufunc->uf_namelen);
Bram Moolenaar313e4722023-02-08 20:55:27 +00001449 if (ufunc->uf_def_status != UF_NOT_COMPILED && cl == NULL)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001450 {
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001451 if (isn_idx == NULL && ufunc->uf_def_status == UF_TO_BE_COMPILED)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001452 // compile the function now, we need the uf_dfunc_idx value
1453 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001454 isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001455 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01001456
1457 // Reserve an extra variable to keep track of the number of closures
1458 // created.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001459 cctx->ctx_has_closure = 1;
1460
1461 // If the referenced function is a closure, it may use items further up in
Bram Moolenaar139575d2022-03-15 19:29:30 +00001462 // the nested context, including this one. But not a function defined at
1463 // the script level.
1464 if ((ufunc->uf_flags & FC_CLOSURE)
1465 && func_name_refcount(cctx->ctx_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001466 cctx->ctx_ufunc->uf_flags |= FC_CLOSURE;
1467
Bram Moolenaar078a4612022-01-04 15:17:03 +00001468 type = ufunc->uf_func_type == NULL ? &t_func_any : ufunc->uf_func_type;
1469 return push_type_stack(cctx, type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001470}
1471
1472/*
1473 * Generate an ISN_NEWFUNC instruction.
1474 * "lambda_name" and "func_name" must be in allocated memory and will be
1475 * consumed.
1476 */
1477 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001478generate_NEWFUNC(
1479 cctx_T *cctx,
1480 char_u *lambda_name,
Bram Moolenaarcc341812022-09-19 15:54:34 +01001481 char_u *func_name)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001482{
1483 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001484 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001485
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001486 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001487 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001488 if ((isn = generate_instr(cctx, ISN_NEWFUNC)) == NULL)
1489 ret = FAIL;
1490 else
1491 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001492 newfuncarg_T *arg = ALLOC_CLEAR_ONE(newfuncarg_T);
1493
1494 if (arg == NULL)
1495 ret = FAIL;
1496 else
1497 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01001498 // Reserve an extra variable to keep track of the number of
1499 // closures created.
1500 cctx->ctx_has_closure = 1;
1501
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001502 isn->isn_arg.newfunc.nf_arg = arg;
1503 arg->nfa_lambda = lambda_name;
1504 arg->nfa_global = func_name;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001505 (void)get_loop_var_info(cctx, &arg->nfa_loopvar_info);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001506 return OK;
1507 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001508 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001509 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001510 vim_free(lambda_name);
1511 vim_free(func_name);
1512 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001513}
1514
1515/*
1516 * Generate an ISN_DEF instruction: list functions
1517 */
1518 int
1519generate_DEF(cctx_T *cctx, char_u *name, size_t len)
1520{
1521 isn_T *isn;
1522
1523 RETURN_OK_IF_SKIP(cctx);
1524 if ((isn = generate_instr(cctx, ISN_DEF)) == NULL)
1525 return FAIL;
1526 if (len > 0)
1527 {
1528 isn->isn_arg.string = vim_strnsave(name, len);
1529 if (isn->isn_arg.string == NULL)
1530 return FAIL;
1531 }
1532 return OK;
1533}
1534
1535/*
1536 * Generate an ISN_JUMP instruction.
1537 */
1538 int
1539generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where)
1540{
1541 isn_T *isn;
1542 garray_T *stack = &cctx->ctx_type_stack;
1543
1544 RETURN_OK_IF_SKIP(cctx);
1545 if ((isn = generate_instr(cctx, ISN_JUMP)) == NULL)
1546 return FAIL;
1547 isn->isn_arg.jump.jump_when = when;
1548 isn->isn_arg.jump.jump_where = where;
1549
1550 if (when != JUMP_ALWAYS && stack->ga_len > 0)
1551 --stack->ga_len;
1552
1553 return OK;
1554}
1555
1556/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001557 * Generate an ISN_WHILE instruction. Similar to ISN_JUMP for :while
1558 */
1559 int
1560generate_WHILE(cctx_T *cctx, int funcref_idx)
1561{
1562 isn_T *isn;
1563 garray_T *stack = &cctx->ctx_type_stack;
1564
1565 RETURN_OK_IF_SKIP(cctx);
1566 if ((isn = generate_instr(cctx, ISN_WHILE)) == NULL)
1567 return FAIL;
1568 isn->isn_arg.whileloop.while_funcref_idx = funcref_idx;
1569 isn->isn_arg.whileloop.while_end = 0; // filled in later
1570
1571 if (stack->ga_len > 0)
1572 --stack->ga_len;
1573
1574 return OK;
1575}
1576
1577/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001578 * Generate an ISN_JUMP_IF_ARG_SET or ISN_JUMP_IF_ARG_NOT_SET instruction.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001579 */
1580 int
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001581generate_JUMP_IF_ARG(cctx_T *cctx, isntype_T isn_type, int arg_off)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001582{
1583 isn_T *isn;
1584
1585 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00001586 if ((isn = generate_instr(cctx, isn_type)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001587 return FAIL;
1588 isn->isn_arg.jumparg.jump_arg_off = arg_off;
1589 // jump_where is set later
1590 return OK;
1591}
1592
1593 int
1594generate_FOR(cctx_T *cctx, int loop_idx)
1595{
1596 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001597
1598 RETURN_OK_IF_SKIP(cctx);
1599 if ((isn = generate_instr(cctx, ISN_FOR)) == NULL)
1600 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001601 isn->isn_arg.forloop.for_loop_idx = loop_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001602
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001603 // type doesn't matter, will be stored next
Bram Moolenaar078a4612022-01-04 15:17:03 +00001604 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001605}
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001606
1607 int
Bram Moolenaarcc341812022-09-19 15:54:34 +01001608generate_ENDLOOP(cctx_T *cctx, loop_info_T *loop_info)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001609{
1610 isn_T *isn;
1611
1612 RETURN_OK_IF_SKIP(cctx);
1613 if ((isn = generate_instr(cctx, ISN_ENDLOOP)) == NULL)
1614 return FAIL;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001615 isn->isn_arg.endloop.end_depth = loop_info->li_depth;
1616 isn->isn_arg.endloop.end_funcref_idx = loop_info->li_funcref_idx;
1617 isn->isn_arg.endloop.end_var_idx = loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001618 isn->isn_arg.endloop.end_var_count =
Bram Moolenaarcc341812022-09-19 15:54:34 +01001619 cctx->ctx_locals.ga_len - loop_info->li_local_count;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001620 return OK;
1621}
1622
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001623/*
1624 * Generate an ISN_TRYCONT instruction.
1625 */
1626 int
1627generate_TRYCONT(cctx_T *cctx, int levels, int where)
1628{
1629 isn_T *isn;
1630
1631 RETURN_OK_IF_SKIP(cctx);
1632 if ((isn = generate_instr(cctx, ISN_TRYCONT)) == NULL)
1633 return FAIL;
1634 isn->isn_arg.trycont.tct_levels = levels;
1635 isn->isn_arg.trycont.tct_where = where;
1636
1637 return OK;
1638}
1639
Bram Moolenaar16900322022-09-08 19:51:45 +01001640/*
1641 * Check "argount" arguments and their types on the type stack.
1642 * Give an error and return FAIL if something is wrong.
1643 * When "method_call" is NULL no code is generated.
1644 */
1645 int
1646check_internal_func_args(
1647 cctx_T *cctx,
1648 int func_idx,
1649 int argcount,
1650 int method_call,
1651 type2_T **argtypes,
1652 type2_T *shuffled_argtypes)
1653{
1654 garray_T *stack = &cctx->ctx_type_stack;
1655 int argoff = check_internal_func(func_idx, argcount);
1656
1657 if (argoff < 0)
1658 return FAIL;
1659
1660 if (method_call && argoff > 1)
1661 {
Bram Moolenaarb7f22702023-04-27 16:24:07 +01001662 if (argcount < argoff)
1663 {
1664 semsg(_(e_not_enough_arguments_for_function_str),
1665 internal_func_name(func_idx));
1666 return FAIL;
1667 }
Bram Moolenaar16900322022-09-08 19:51:45 +01001668
Bram Moolenaarb7f22702023-04-27 16:24:07 +01001669 isn_T *isn = generate_instr(cctx, ISN_SHUFFLE);
Bram Moolenaar16900322022-09-08 19:51:45 +01001670 if (isn == NULL)
1671 return FAIL;
1672 isn->isn_arg.shuffle.shfl_item = argcount;
1673 isn->isn_arg.shuffle.shfl_up = argoff - 1;
1674 }
1675
1676 if (argcount > 0)
1677 {
1678 type2_T *typep = ((type2_T *)stack->ga_data) + stack->ga_len - argcount;
1679
1680 // Check the types of the arguments.
1681 if (method_call && argoff > 1)
1682 {
1683 int i;
1684
1685 for (i = 0; i < argcount; ++i)
1686 shuffled_argtypes[i] = (i < argoff - 1)
1687 ? typep[i + 1]
1688 : (i == argoff - 1) ? typep[0] : typep[i];
1689 *argtypes = shuffled_argtypes;
1690 }
1691 else
1692 {
1693 int i;
1694
1695 for (i = 0; i < argcount; ++i)
1696 shuffled_argtypes[i] = typep[i];
1697 *argtypes = shuffled_argtypes;
1698 }
1699 if (internal_func_check_arg_types(*argtypes, func_idx, argcount,
1700 cctx) == FAIL)
1701 return FAIL;
1702 }
1703 return OK;
1704}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001705
1706/*
1707 * Generate an ISN_BCALL instruction.
1708 * "method_call" is TRUE for "value->method()"
1709 * Return FAIL if the number of arguments is wrong.
1710 */
1711 int
1712generate_BCALL(cctx_T *cctx, int func_idx, int argcount, int method_call)
1713{
1714 isn_T *isn;
1715 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001716 type2_T *argtypes = NULL;
1717 type2_T shuffled_argtypes[MAX_FUNC_ARGS];
1718 type2_T *maptype = NULL;
1719 type_T *type;
Bram Moolenaar81330182022-02-01 12:11:58 +00001720 type_T *decl_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001721
1722 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaar16900322022-09-08 19:51:45 +01001723
1724 if (check_internal_func_args(cctx, func_idx, argcount, method_call,
1725 &argtypes, shuffled_argtypes) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001726 return FAIL;
1727
Bram Moolenaar16900322022-09-08 19:51:45 +01001728 if (internal_func_is_map(func_idx))
1729 maptype = argtypes;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001730
1731 if ((isn = generate_instr(cctx, ISN_BCALL)) == NULL)
1732 return FAIL;
1733 isn->isn_arg.bfunc.cbf_idx = func_idx;
1734 isn->isn_arg.bfunc.cbf_argcount = argcount;
1735
1736 // Drop the argument types and push the return type.
1737 stack->ga_len -= argcount;
Bram Moolenaar32517c42023-01-15 18:17:12 +00001738 type = internal_func_ret_type(func_idx, argcount, argtypes, &decl_type,
1739 cctx->ctx_type_list);
Bram Moolenaar81330182022-02-01 12:11:58 +00001740 if (push_type_stack2(cctx, type, decl_type) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001741 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001742
Bram Moolenaar35c807d2022-01-27 16:36:29 +00001743 if (maptype != NULL && maptype[0].type_decl->tt_member != NULL
1744 && maptype[0].type_decl->tt_member != &t_any)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001745 // Check that map() didn't change the item types.
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001746 generate_TYPECHECK(cctx, maptype[0].type_decl, FALSE, -1, FALSE, 1);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001747
1748 return OK;
1749}
1750
1751/*
1752 * Generate an ISN_LISTAPPEND instruction. Works like add().
1753 * Argument count is already checked.
1754 */
1755 int
1756generate_LISTAPPEND(cctx_T *cctx)
1757{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001758 type_T *list_type;
1759 type_T *item_type;
1760 type_T *expected;
1761
1762 // Caller already checked that list_type is a list.
Bram Moolenaar81330182022-02-01 12:11:58 +00001763 // For checking the item type we use the declared type of the list and the
1764 // current type of the added item, adding a string to [1, 2] is OK.
1765 list_type = get_decl_type_on_stack(cctx, 1);
Bram Moolenaarfa103972022-09-29 19:14:42 +01001766 if (arg_type_modifiable(list_type, 1) == FAIL)
1767 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001768 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001769 expected = list_type->tt_member;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001770 if (need_type(item_type, expected, FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001771 return FAIL;
1772
1773 if (generate_instr(cctx, ISN_LISTAPPEND) == NULL)
1774 return FAIL;
1775
Bram Moolenaar078a4612022-01-04 15:17:03 +00001776 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001777 return OK;
1778}
1779
1780/*
1781 * Generate an ISN_BLOBAPPEND instruction. Works like add().
1782 * Argument count is already checked.
1783 */
1784 int
1785generate_BLOBAPPEND(cctx_T *cctx)
1786{
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001787 type_T *item_type;
1788
Bram Moolenaarfa103972022-09-29 19:14:42 +01001789 // Caller already checked that blob_type is a blob, check it is modifiable.
1790 if (arg_type_modifiable(get_decl_type_on_stack(cctx, 1), 1) == FAIL)
1791 return FAIL;
Bram Moolenaar078a4612022-01-04 15:17:03 +00001792 item_type = get_type_on_stack(cctx, 0);
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001793 if (need_type(item_type, &t_number, FALSE,
1794 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001795 return FAIL;
1796
1797 if (generate_instr(cctx, ISN_BLOBAPPEND) == NULL)
1798 return FAIL;
1799
Bram Moolenaar078a4612022-01-04 15:17:03 +00001800 --cctx->ctx_type_stack.ga_len; // drop the argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001801 return OK;
1802}
1803
1804/*
Bram Moolenaard0200c82023-01-28 15:19:40 +00001805 * Generate an ISN_DCALL, ISN_UCALL or ISN_METHODCALL instruction.
1806 * When calling a method on an object, of which we know the interface only,
1807 * then "cl" is the interface and "mi" the method index on the interface.
Ernie Rael58c95792024-08-13 23:27:22 +02001808 * save is_super in the "isn->isn_arg"; it flags execution to use mfunc
1809 * directly to determine ufunc.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001810 * Return FAIL if the number of arguments is wrong.
1811 */
1812 int
Bram Moolenaard0200c82023-01-28 15:19:40 +00001813generate_CALL(
1814 cctx_T *cctx,
1815 ufunc_T *ufunc,
1816 class_T *cl,
1817 int mi,
Ernie Rael58c95792024-08-13 23:27:22 +02001818 int pushed_argcount,
1819 int is_super)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001820{
1821 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001822 int regular_args = ufunc->uf_args.ga_len;
1823 int argcount = pushed_argcount;
1824
1825 RETURN_OK_IF_SKIP(cctx);
1826 if (argcount > regular_args && !has_varargs(ufunc))
1827 {
1828 semsg(_(e_too_many_arguments_for_function_str),
1829 printable_func_name(ufunc));
1830 return FAIL;
1831 }
1832 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1833 {
1834 semsg(_(e_not_enough_arguments_for_function_str),
1835 printable_func_name(ufunc));
1836 return FAIL;
1837 }
1838
1839 if (ufunc->uf_def_status != UF_NOT_COMPILED
1840 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1841 {
1842 int i;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001843 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001844
1845 for (i = 0; i < argcount; ++i)
1846 {
1847 type_T *expected;
1848 type_T *actual;
1849
Bram Moolenaar078a4612022-01-04 15:17:03 +00001850 actual = get_type_on_stack(cctx, argcount - i - 1);
Ernie Raelb077b582023-12-14 20:11:44 +01001851 if (check_type_is_value(actual) == FAIL)
1852 return FAIL;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001853 if (actual->tt_type == VAR_SPECIAL
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001854 && i >= regular_args - ufunc->uf_def_args.ga_len)
1855 {
1856 // assume v:none used for default argument value
1857 continue;
1858 }
1859 if (i < regular_args)
1860 {
1861 if (ufunc->uf_arg_types == NULL)
1862 continue;
1863 expected = ufunc->uf_arg_types[i];
1864 }
1865 else if (ufunc->uf_va_type == NULL
1866 || ufunc->uf_va_type == &t_list_any)
1867 // possibly a lambda or "...: any"
1868 expected = &t_any;
1869 else
1870 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001871 if (need_type(actual, expected, FALSE,
1872 -argcount + i, i + 1, cctx, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001873 {
1874 arg_type_mismatch(expected, actual, i + 1);
1875 return FAIL;
1876 }
1877 }
Bram Moolenaar139575d2022-03-15 19:29:30 +00001878 compile_type = get_compile_type(ufunc);
1879 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001880 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaar139575d2022-03-15 19:29:30 +00001881 compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001882 return FAIL;
1883 }
1884 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1885 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001886 emsg_funcname(e_call_to_function_that_failed_to_compile_str,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001887 ufunc->uf_name);
1888 return FAIL;
1889 }
1890
Bram Moolenaard0200c82023-01-28 15:19:40 +00001891 if ((isn = generate_instr(cctx, cl != NULL ? ISN_METHODCALL
1892 : ufunc->uf_def_status != UF_NOT_COMPILED
1893 ? ISN_DCALL : ISN_UCALL)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001894 return FAIL;
Bram Moolenaar836137d2023-01-29 14:11:24 +00001895 if (cl != NULL /* isn->isn_type == ISN_METHODCALL */)
Bram Moolenaard0200c82023-01-28 15:19:40 +00001896 {
1897 isn->isn_arg.mfunc = ALLOC_ONE(cmfunc_T);
1898 if (isn->isn_arg.mfunc == NULL)
1899 return FAIL;
1900 isn->isn_arg.mfunc->cmf_itf = cl;
1901 ++cl->class_refcount;
1902 isn->isn_arg.mfunc->cmf_idx = mi;
1903 isn->isn_arg.mfunc->cmf_argcount = argcount;
Ernie Rael58c95792024-08-13 23:27:22 +02001904 isn->isn_arg.mfunc->cmf_is_super = is_super;
Bram Moolenaard0200c82023-01-28 15:19:40 +00001905 }
1906 else if (isn->isn_type == ISN_DCALL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001907 {
1908 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1909 isn->isn_arg.dfunc.cdf_argcount = argcount;
1910 }
1911 else
1912 {
1913 // A user function may be deleted and redefined later, can't use the
1914 // ufunc pointer, need to look it up again at runtime.
John Marriottb32800f2025-02-01 15:25:34 +01001915 isn->isn_arg.ufunc.cuf_name = vim_strnsave(ufunc->uf_name, ufunc->uf_namelen);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001916 isn->isn_arg.ufunc.cuf_argcount = argcount;
1917 }
1918
Bram Moolenaar078a4612022-01-04 15:17:03 +00001919 // drop the argument types
1920 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001921
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02001922 // For an object or class method call, drop the object/class type.
Yegappan Lakshmanan639751d2023-08-27 19:23:37 +02001923 if (ufunc->uf_class != NULL)
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02001924 {
1925 // When a class method is called without the class name prefix, then
1926 // the type will not be in the stack.
1927 type_T *stype = get_type_on_stack(cctx, 0);
1928 if (stype->tt_type == VAR_CLASS || stype->tt_type == VAR_OBJECT)
1929 cctx->ctx_type_stack.ga_len--;
1930 }
Yegappan Lakshmanan639751d2023-08-27 19:23:37 +02001931
Bram Moolenaar078a4612022-01-04 15:17:03 +00001932 // add return type
1933 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001934}
1935
1936/*
1937 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1938 */
1939 int
1940generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1941{
1942 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001943
1944 RETURN_OK_IF_SKIP(cctx);
1945 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1946 return FAIL;
1947 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1948 isn->isn_arg.ufunc.cuf_argcount = argcount;
1949
Bram Moolenaar078a4612022-01-04 15:17:03 +00001950 // drop the argument types
1951 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001952
Bram Moolenaar078a4612022-01-04 15:17:03 +00001953 // add return value
1954 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001955}
1956
1957/*
Bram Moolenaar16900322022-09-08 19:51:45 +01001958 * Check the arguments of function "type" against the types on the stack.
1959 * Returns OK or FAIL;
1960 */
1961 int
1962check_func_args_from_type(
1963 cctx_T *cctx,
1964 type_T *type,
1965 int argcount,
1966 int at_top,
1967 char_u *name)
1968{
1969 if (type->tt_argcount != -1)
1970 {
1971 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1972
1973 if (argcount < type->tt_min_argcount - varargs)
1974 {
1975 emsg_funcname(e_not_enough_arguments_for_function_str, name);
1976 return FAIL;
1977 }
1978 if (!varargs && argcount > type->tt_argcount)
1979 {
1980 emsg_funcname(e_too_many_arguments_for_function_str, name);
1981 return FAIL;
1982 }
1983 if (type->tt_args != NULL)
1984 {
1985 int i;
1986
1987 for (i = 0; i < argcount; ++i)
1988 {
1989 int offset = -argcount + i - (at_top ? 0 : 1);
1990 type_T *actual = get_type_on_stack(cctx, -1 - offset);
1991 type_T *expected;
1992
Ernie Raelb077b582023-12-14 20:11:44 +01001993 if (check_type_is_value(actual) == FAIL)
1994 return FAIL;
Bram Moolenaar16900322022-09-08 19:51:45 +01001995 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaar36818a92023-01-03 12:33:26 +00001996 {
1997 expected = type->tt_args[type->tt_argcount - 1];
1998 if (expected != NULL && expected->tt_type == VAR_LIST)
1999 expected = expected->tt_member;
2000 if (expected == NULL)
2001 expected = &t_any;
2002 }
Bram Moolenaar16900322022-09-08 19:51:45 +01002003 else if (i >= type->tt_min_argcount
2004 && actual->tt_type == VAR_SPECIAL)
2005 expected = &t_any;
2006 else
2007 expected = type->tt_args[i];
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002008 if (need_type(actual, expected, FALSE,
2009 offset, i + 1, cctx, TRUE, FALSE) == FAIL)
Bram Moolenaar16900322022-09-08 19:51:45 +01002010 {
2011 arg_type_mismatch(expected, actual, i + 1);
2012 return FAIL;
2013 }
2014 }
2015 }
2016 }
2017
2018 return OK;
2019}
2020/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002021 * Generate an ISN_PCALL instruction.
2022 * "type" is the type of the FuncRef.
2023 */
2024 int
2025generate_PCALL(
2026 cctx_T *cctx,
2027 int argcount,
2028 char_u *name,
2029 type_T *type,
2030 int at_top)
2031{
2032 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002033 type_T *ret_type;
2034
2035 RETURN_OK_IF_SKIP(cctx);
2036
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002037 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002038 ret_type = &t_any;
2039 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
2040 {
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002041 if (check_func_args_from_type(cctx, type, argcount, at_top, name)
2042 == FAIL)
Bram Moolenaar16900322022-09-08 19:51:45 +01002043 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002044
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002045 ret_type = type->tt_member;
2046 if (ret_type == &t_unknown)
2047 // return type not known yet, use a runtime check
2048 ret_type = &t_any;
2049 }
2050 else
2051 {
2052 semsg(_(e_not_callable_type_str), name);
2053 return FAIL;
2054 }
2055
2056 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2057 return FAIL;
2058 isn->isn_arg.pfunc.cpf_top = at_top;
2059 isn->isn_arg.pfunc.cpf_argcount = argcount;
2060
Bram Moolenaar078a4612022-01-04 15:17:03 +00002061 // drop the arguments and the funcref/partial
2062 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002063
Bram Moolenaar078a4612022-01-04 15:17:03 +00002064 // push the return value
2065 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002066
2067 // If partial is above the arguments it must be cleared and replaced with
2068 // the return value.
2069 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2070 return FAIL;
2071
2072 return OK;
2073}
2074
2075/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002076 * Generate an ISN_DEFER instruction.
2077 */
2078 int
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02002079generate_DEFER(cctx_T *cctx, int var_idx, int argcount)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002080{
2081 isn_T *isn;
2082
2083 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02002084 if ((isn = generate_instr_drop(cctx, ISN_DEFER, argcount + 1)) == NULL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002085 return FAIL;
2086 isn->isn_arg.defer.defer_var_idx = var_idx;
2087 isn->isn_arg.defer.defer_argcount = argcount;
2088 return OK;
2089}
2090
2091/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002092 * Generate an ISN_STRINGMEMBER instruction.
2093 */
2094 int
2095generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
2096{
2097 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002098 type_T *type;
2099
2100 RETURN_OK_IF_SKIP(cctx);
2101 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
2102 return FAIL;
2103 isn->isn_arg.string = vim_strnsave(name, len);
2104
2105 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00002106 type = get_type_on_stack(cctx, 0);
Bram Moolenaar0089ce22022-10-08 14:39:36 +01002107 if (type->tt_type != VAR_DICT
2108 && type->tt_type != VAR_ANY && type->tt_type != VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002109 {
2110 char *tofree;
2111
2112 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
2113 name, type_name(type, &tofree));
2114 vim_free(tofree);
2115 return FAIL;
2116 }
2117 // change dict type to dict member type
2118 if (type->tt_type == VAR_DICT)
2119 {
Bram Moolenaar0089ce22022-10-08 14:39:36 +01002120 type_T *ntype = type->tt_member->tt_type == VAR_UNKNOWN
Bram Moolenaar078a4612022-01-04 15:17:03 +00002121 ? &t_any : type->tt_member;
2122 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002123 }
2124
2125 return OK;
2126}
2127
2128/*
2129 * Generate an ISN_ECHO instruction.
2130 */
2131 int
2132generate_ECHO(cctx_T *cctx, int with_white, int count)
2133{
2134 isn_T *isn;
2135
2136 RETURN_OK_IF_SKIP(cctx);
2137 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2138 return FAIL;
2139 isn->isn_arg.echo.echo_with_white = with_white;
2140 isn->isn_arg.echo.echo_count = count;
2141
2142 return OK;
2143}
2144
2145/*
2146 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
2147 */
2148 int
2149generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
2150{
2151 isn_T *isn;
2152
Bram Moolenaar2eae3d22022-10-07 15:09:27 +01002153 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002154 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
2155 return FAIL;
2156 isn->isn_arg.number = count;
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002157 return OK;
2158}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002159
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002160/*
2161 * Generate an ISN_ECHOWINDOW instruction
2162 */
2163 int
2164generate_ECHOWINDOW(cctx_T *cctx, int count, long time)
2165{
2166 isn_T *isn;
2167
2168 if ((isn = generate_instr_drop(cctx, ISN_ECHOWINDOW, count)) == NULL)
2169 return FAIL;
2170 isn->isn_arg.echowin.ewin_count = count;
2171 isn->isn_arg.echowin.ewin_time = time;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002172 return OK;
2173}
2174
2175/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002176 * Generate an ISN_SOURCE instruction.
2177 */
2178 int
2179generate_SOURCE(cctx_T *cctx, int sid)
2180{
2181 isn_T *isn;
2182
2183 if ((isn = generate_instr(cctx, ISN_SOURCE)) == NULL)
2184 return FAIL;
2185 isn->isn_arg.number = sid;
2186
2187 return OK;
2188}
2189
2190/*
64-bitmane08f10a2025-03-18 22:14:34 +01002191 * Generate an ISN_PUT or ISN_IPUT instruction depending on fixindent.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002192 */
2193 int
64-bitmane08f10a2025-03-18 22:14:34 +01002194generate_PUT(cctx_T *cctx, int regname, linenr_T lnum, int fixindent)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002195{
2196 isn_T *isn;
2197
2198 RETURN_OK_IF_SKIP(cctx);
64-bitmane08f10a2025-03-18 22:14:34 +01002199 isn = (fixindent) ? generate_instr(cctx, ISN_IPUT) :
2200 generate_instr(cctx, ISN_PUT);
2201 if (isn == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002202 return FAIL;
2203 isn->isn_arg.put.put_regname = regname;
2204 isn->isn_arg.put.put_lnum = lnum;
2205 return OK;
2206}
2207
2208/*
Ernie Rael64885642023-10-04 20:16:22 +02002209 * Generate a LOCKUNLOCK instruction.The root item, where the indexing starts
2210 * to find the variable, is on the stack. The instr takes
2211 * - the string to parse, "root.b[idx1][idx2].d.val", to find the variable
2212 * - the class, if any, in which the string executes.
2213 * - if the root item is a function argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002214 * A copy is made of "line".
2215 */
2216 int
Ernie Raelee865f32023-09-29 19:53:55 +02002217generate_LOCKUNLOCK(cctx_T *cctx, char_u *line, int is_arg)
2218{
2219 isn_T *isn;
2220
2221 RETURN_OK_IF_SKIP(cctx);
2222 if ((isn = generate_instr(cctx, ISN_LOCKUNLOCK)) == NULL)
2223 return FAIL;
Ernie Rael64885642023-10-04 20:16:22 +02002224 class_T *cl = cctx->ctx_ufunc != NULL ? cctx->ctx_ufunc->uf_class : NULL;
2225 isn->isn_arg.lockunlock.lu_string = vim_strsave(line);
2226 isn->isn_arg.lockunlock.lu_cl_exec = cl;
2227 if (cl != NULL)
2228 ++cl->class_refcount;
2229 isn->isn_arg.lockunlock.lu_is_arg = is_arg;
Ernie Raelee865f32023-09-29 19:53:55 +02002230 return OK;
2231}
2232
2233/*
2234 * Generate an EXEC instruction that takes a string argument.
2235 * A copy is made of "line".
2236 */
2237 int
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002238generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
2239{
2240 isn_T *isn;
2241
2242 RETURN_OK_IF_SKIP(cctx);
2243 if ((isn = generate_instr(cctx, isntype)) == NULL)
2244 return FAIL;
2245 isn->isn_arg.string = vim_strsave(line);
2246 return OK;
2247}
2248
2249/*
2250 * Generate an EXEC instruction that takes a string argument.
2251 * "str" must be allocated, it is consumed.
2252 */
2253 int
2254generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
2255{
2256 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002257 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002258
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002259 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002260 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002261 if ((isn = generate_instr(cctx, isntype)) == NULL)
2262 ret = FAIL;
2263 else
2264 {
2265 isn->isn_arg.string = str;
2266 return OK;
2267 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002268 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002269 vim_free(str);
2270 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002271}
2272
2273 int
2274generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2275{
2276 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002277
2278 RETURN_OK_IF_SKIP(cctx);
2279 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2280 return FAIL;
2281 isn->isn_arg.string = vim_strsave(line);
2282
Bram Moolenaar078a4612022-01-04 15:17:03 +00002283 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002284}
2285
2286 int
2287generate_EXECCONCAT(cctx_T *cctx, int count)
2288{
2289 isn_T *isn;
2290
2291 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2292 return FAIL;
2293 isn->isn_arg.number = count;
2294 return OK;
2295}
2296
2297/*
2298 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2299 */
2300 int
2301generate_RANGE(cctx_T *cctx, char_u *range)
2302{
2303 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002304
2305 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2306 return FAIL;
2307 isn->isn_arg.string = range;
2308
Bram Moolenaar078a4612022-01-04 15:17:03 +00002309 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002310}
2311
2312 int
2313generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2314{
2315 isn_T *isn;
2316
2317 RETURN_OK_IF_SKIP(cctx);
2318 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2319 return FAIL;
2320 isn->isn_arg.unpack.unp_count = var_count;
2321 isn->isn_arg.unpack.unp_semicolon = semicolon;
2322 return OK;
2323}
2324
2325/*
2326 * Generate an instruction for any command modifiers.
2327 */
2328 int
2329generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
2330{
2331 isn_T *isn;
2332
2333 if (has_cmdmod(cmod, FALSE))
2334 {
2335 cctx->ctx_has_cmdmod = TRUE;
2336
2337 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
2338 return FAIL;
2339 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2340 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2341 return FAIL;
2342 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
2343 // filter program now belongs to the instruction
2344 cmod->cmod_filter_regmatch.regprog = NULL;
2345 }
2346
2347 return OK;
2348}
2349
2350 int
2351generate_undo_cmdmods(cctx_T *cctx)
2352{
2353 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2354 return FAIL;
2355 cctx->ctx_has_cmdmod = FALSE;
2356 return OK;
2357}
2358
2359/*
2360 * Generate a STORE instruction for "dest", not being "dest_local".
Bram Moolenaard505d172022-12-18 21:42:55 +00002361 * "lhs" might be NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002362 * Return FAIL when out of memory.
2363 */
2364 int
2365generate_store_var(
2366 cctx_T *cctx,
2367 assign_dest_T dest,
2368 int opt_flags,
2369 int vimvaridx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002370 type_T *type,
Bram Moolenaard505d172022-12-18 21:42:55 +00002371 char_u *name,
2372 lhs_T *lhs)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002373{
2374 switch (dest)
2375 {
2376 case dest_option:
2377 return generate_STOREOPT(cctx, ISN_STOREOPT,
2378 skip_option_env_lead(name), opt_flags);
2379 case dest_func_option:
2380 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
2381 skip_option_env_lead(name), opt_flags);
2382 case dest_global:
2383 // include g: with the name, easier to execute that way
2384 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
2385 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
2386 case dest_buffer:
2387 // include b: with the name, easier to execute that way
2388 return generate_STORE(cctx, ISN_STOREB, 0, name);
2389 case dest_window:
2390 // include w: with the name, easier to execute that way
2391 return generate_STORE(cctx, ISN_STOREW, 0, name);
2392 case dest_tab:
2393 // include t: with the name, easier to execute that way
2394 return generate_STORE(cctx, ISN_STORET, 0, name);
2395 case dest_env:
2396 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
2397 case dest_reg:
2398 return generate_STORE(cctx, ISN_STOREREG,
2399 name[1] == '@' ? '"' : name[1], NULL);
2400 case dest_vimvar:
2401 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
2402 case dest_script:
Ernie Rael3f821d62024-04-24 20:07:50 +02002403 case dest_script_v9:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002404 {
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002405 int scriptvar_idx = lhs->lhs_scriptvar_idx;
2406 int scriptvar_sid = lhs->lhs_scriptvar_sid;
2407 if (scriptvar_idx < 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002408 {
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002409 isntype_T isn_type = ISN_STORES;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002410
Ernie Rael3f821d62024-04-24 20:07:50 +02002411 // If "sn_import_autoload", generate ISN_STOREEXPORT (not
2412 // ISN_STORES) if destination is in a vim9script or if
2413 // there is no "sn_autoload_prefix".
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002414 if (SCRIPT_ID_VALID(scriptvar_sid)
2415 && SCRIPT_ITEM(scriptvar_sid)->sn_import_autoload
Ernie Rael3f821d62024-04-24 20:07:50 +02002416 && ((SCRIPT_ITEM(scriptvar_sid)
2417 ->sn_autoload_prefix == NULL)
2418 || lhs->lhs_dest == dest_script_v9))
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002419 {
2420 // "import autoload './dir/script.vim'" - load script
2421 // first
2422 if (generate_SOURCE(cctx, scriptvar_sid) == FAIL)
2423 return FAIL;
2424 isn_type = ISN_STOREEXPORT;
2425 }
2426
2427 // "s:" may be included in the name.
2428 return generate_OLDSCRIPT(cctx, isn_type, name,
2429 scriptvar_sid, type);
2430 }
2431 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002432 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002433 }
Bram Moolenaard505d172022-12-18 21:42:55 +00002434 case dest_class_member:
2435 return generate_CLASSMEMBER(cctx, FALSE,
2436 lhs->lhs_class, lhs->lhs_classmember_idx);
2437
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002438 case dest_local:
2439 case dest_expr:
2440 // cannot happen
2441 break;
2442 }
2443 return FAIL;
2444}
2445
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002446/*
2447 * Return TRUE when inside a "for" or "while" loop.
2448 */
2449 int
2450inside_loop_scope(cctx_T *cctx)
2451{
2452 scope_T *scope = cctx->ctx_scope;
2453
2454 for (;;)
2455 {
2456 if (scope == NULL)
2457 break;
2458 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
2459 return TRUE;
2460 scope = scope->se_outer;
2461 }
2462 return FALSE;
2463}
2464
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002465 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002466generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002467{
2468 if (lhs->lhs_dest != dest_local)
2469 return generate_store_var(cctx, lhs->lhs_dest,
2470 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
Bram Moolenaard505d172022-12-18 21:42:55 +00002471 lhs->lhs_type, lhs->lhs_name, lhs);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002472
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002473 if (lhs->lhs_lvar == NULL)
2474 return OK;
2475
2476 garray_T *instr = &cctx->ctx_instr;
2477 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2478
2479 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
2480 // ISN_STORENR.
2481 // And "var = 0" does not need any instruction.
2482 if (lhs->lhs_lvar->lv_from_outer == 0
2483 && instr->ga_len == instr_count + 1
2484 && isn->isn_type == ISN_PUSHNR)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002485 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002486 varnumber_T val = isn->isn_arg.number;
2487 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002488
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002489 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002490 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002491 // zero is the default value, no need to do anything
2492 --instr->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002493 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002494 else
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002495 {
2496 isn->isn_type = ISN_STORENR;
2497 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
2498 isn->isn_arg.storenr.stnr_val = val;
2499 }
2500 if (stack->ga_len > 0)
2501 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002502 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002503 else if (lhs->lhs_lvar->lv_from_outer > 0)
2504 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
2505 lhs->lhs_lvar->lv_from_outer, lhs->lhs_lvar->lv_loop_idx);
2506 else
2507 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002508 return OK;
2509}
2510
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +01002511/*
2512 * Generate instruction to set the script context. Used to evaluate an
2513 * object member variable initialization expression in the context of the
2514 * script where the class is defined.
2515 */
2516 int
2517generate_SCRIPTCTX_SET(cctx_T *cctx, sctx_T new_sctx)
2518{
2519 isn_T *isn;
2520
2521 RETURN_OK_IF_SKIP(cctx);
2522 if ((isn = generate_instr(cctx, ISN_SCRIPTCTX_SET)) == NULL)
2523 return FAIL;
2524 isn->isn_arg.setsctx = new_sctx;
2525 return OK;
2526}
2527
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002528#if defined(FEAT_PROFILE) || defined(PROTO)
2529 void
2530may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2531{
2532 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
2533 generate_instr(cctx, ISN_PROF_END);
2534}
2535#endif
2536
2537
2538/*
2539 * Delete an instruction, free what it contains.
2540 */
2541 void
2542delete_instr(isn_T *isn)
2543{
2544 switch (isn->isn_type)
2545 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002546 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002547 case ISN_DEF:
2548 case ISN_EXEC:
2549 case ISN_EXECRANGE:
2550 case ISN_EXEC_SPLIT:
2551 case ISN_LEGACY_EVAL:
2552 case ISN_LOADAUTO:
2553 case ISN_LOADB:
2554 case ISN_LOADENV:
2555 case ISN_LOADG:
2556 case ISN_LOADOPT:
2557 case ISN_LOADT:
2558 case ISN_LOADW:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002559 case ISN_PUSHEXC:
2560 case ISN_PUSHFUNC:
2561 case ISN_PUSHS:
2562 case ISN_RANGE:
2563 case ISN_STOREAUTO:
2564 case ISN_STOREB:
2565 case ISN_STOREENV:
2566 case ISN_STOREG:
2567 case ISN_STORET:
2568 case ISN_STOREW:
2569 case ISN_STRINGMEMBER:
2570 vim_free(isn->isn_arg.string);
2571 break;
2572
Ernie Rael64885642023-10-04 20:16:22 +02002573 case ISN_LOCKUNLOCK:
2574 class_unref(isn->isn_arg.lockunlock.lu_cl_exec);
2575 vim_free(isn->isn_arg.lockunlock.lu_string);
2576 break;
2577
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002578 case ISN_SUBSTITUTE:
2579 {
2580 int idx;
2581 isn_T *list = isn->isn_arg.subs.subs_instr;
2582
2583 vim_free(isn->isn_arg.subs.subs_cmd);
2584 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2585 delete_instr(list + idx);
2586 vim_free(list);
2587 }
2588 break;
2589
2590 case ISN_INSTR:
2591 {
2592 int idx;
2593 isn_T *list = isn->isn_arg.instr;
2594
2595 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2596 delete_instr(list + idx);
2597 vim_free(list);
2598 }
2599 break;
2600
2601 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002602 case ISN_LOADEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002603 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002604 case ISN_STOREEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002605 vim_free(isn->isn_arg.loadstore.ls_name);
2606 break;
2607
2608 case ISN_UNLET:
2609 case ISN_UNLETENV:
2610 vim_free(isn->isn_arg.unlet.ul_name);
2611 break;
2612
2613 case ISN_STOREOPT:
2614 case ISN_STOREFUNCOPT:
2615 vim_free(isn->isn_arg.storeopt.so_name);
2616 break;
2617
2618 case ISN_PUSHBLOB: // push blob isn_arg.blob
2619 blob_unref(isn->isn_arg.blob);
2620 break;
2621
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002622 case ISN_PUSHCLASS:
Bram Moolenaar30a84472023-02-27 08:07:14 +00002623 class_unref(isn->isn_arg.classarg);
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002624 break;
2625
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002626 case ISN_UCALL:
2627 vim_free(isn->isn_arg.ufunc.cuf_name);
2628 break;
2629
2630 case ISN_FUNCREF:
2631 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002632 funcref_T *funcref = &isn->isn_arg.funcref;
2633 funcref_extra_T *extra = funcref->fr_extra;
2634
2635 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002636 {
2637 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002638 + funcref->fr_dfunc_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002639 ufunc_T *ufunc = dfunc->df_ufunc;
2640
2641 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2642 func_ptr_unref(ufunc);
2643 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002644 if (extra != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002645 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002646 char_u *name = extra->fre_func_name;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002647
2648 if (name != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002649 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002650 func_unref(name);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002651 vim_free(name);
2652 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00002653 if (extra->fre_class != NULL)
2654 class_unref(extra->fre_class);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002655 vim_free(extra);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002656 }
2657 }
2658 break;
2659
2660 case ISN_DCALL:
2661 {
2662 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002663 + isn->isn_arg.dfunc.cdf_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002664
2665 if (dfunc->df_ufunc != NULL
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002666 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002667 func_ptr_unref(dfunc->df_ufunc);
2668 }
2669 break;
2670
Bram Moolenaard0200c82023-01-28 15:19:40 +00002671 case ISN_METHODCALL:
2672 {
2673 cmfunc_T *mfunc = isn->isn_arg.mfunc;
2674 class_unref(mfunc->cmf_itf);
2675 vim_free(mfunc);
2676 }
2677 break;
2678
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002679 case ISN_NEWFUNC:
2680 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002681 newfuncarg_T *arg = isn->isn_arg.newfunc.nf_arg;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002682
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002683 if (arg != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002684 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002685 ufunc_T *ufunc = find_func_even_dead(
2686 arg->nfa_lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002687
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002688 if (ufunc != NULL)
2689 {
2690 unlink_def_function(ufunc);
2691 func_ptr_unref(ufunc);
2692 }
2693
2694 vim_free(arg->nfa_lambda);
2695 vim_free(arg->nfa_global);
2696 vim_free(arg);
2697 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002698 }
2699 break;
2700
2701 case ISN_CHECKTYPE:
2702 case ISN_SETTYPE:
2703 free_type(isn->isn_arg.type.ct_type);
2704 break;
2705
2706 case ISN_CMDMOD:
2707 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002708 ->cmod_filter_regmatch.regprog);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002709 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2710 break;
2711
2712 case ISN_LOADSCRIPT:
2713 case ISN_STORESCRIPT:
2714 vim_free(isn->isn_arg.script.scriptref);
2715 break;
2716
Bram Moolenaard505d172022-12-18 21:42:55 +00002717 case ISN_LOAD_CLASSMEMBER:
2718 case ISN_STORE_CLASSMEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00002719 case ISN_GET_ITF_MEMBER:
Bram Moolenaard505d172022-12-18 21:42:55 +00002720 class_unref(isn->isn_arg.classmember.cm_class);
2721 break;
2722
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002723 case ISN_STOREINDEX:
2724 class_unref(isn->isn_arg.storeindex.si_class);
2725 break;
2726
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002727 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002728 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002729 break;
2730
2731 case ISN_CEXPR_CORE:
2732 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2733 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2734 break;
2735
2736 case ISN_2BOOL:
2737 case ISN_2STRING:
2738 case ISN_2STRING_ANY:
2739 case ISN_ADDBLOB:
2740 case ISN_ADDLIST:
2741 case ISN_ANYINDEX:
2742 case ISN_ANYSLICE:
2743 case ISN_BCALL:
2744 case ISN_BLOBAPPEND:
2745 case ISN_BLOBINDEX:
2746 case ISN_BLOBSLICE:
2747 case ISN_CATCH:
2748 case ISN_CEXPR_AUCMD:
2749 case ISN_CHECKLEN:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002750 case ISN_CLEARDICT:
2751 case ISN_CMDMOD_REV:
2752 case ISN_COMPAREANY:
2753 case ISN_COMPAREBLOB:
2754 case ISN_COMPAREBOOL:
2755 case ISN_COMPAREDICT:
2756 case ISN_COMPAREFLOAT:
2757 case ISN_COMPAREFUNC:
2758 case ISN_COMPARELIST:
2759 case ISN_COMPARENR:
Bram Moolenaar7a222242022-03-01 19:23:24 +00002760 case ISN_COMPARENULL:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002761 case ISN_COMPAREOBJECT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002762 case ISN_COMPARESPECIAL:
2763 case ISN_COMPARESTRING:
2764 case ISN_CONCAT:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002765 case ISN_CONSTRUCT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002766 case ISN_COND2BOOL:
2767 case ISN_DEBUG:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002768 case ISN_DEFER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002769 case ISN_DROP:
2770 case ISN_ECHO:
2771 case ISN_ECHOCONSOLE:
2772 case ISN_ECHOERR:
2773 case ISN_ECHOMSG:
Bram Moolenaar68a635a2022-09-01 17:26:17 +01002774 case ISN_ECHOWINDOW:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002775 case ISN_ENDLOOP:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002776 case ISN_ENDTRY:
2777 case ISN_EXECCONCAT:
2778 case ISN_EXECUTE:
2779 case ISN_FINALLY:
2780 case ISN_FINISH:
2781 case ISN_FOR:
2782 case ISN_GETITEM:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002783 case ISN_GET_OBJ_MEMBER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002784 case ISN_JUMP:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002785 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002786 case ISN_JUMP_IF_ARG_SET:
2787 case ISN_LISTAPPEND:
2788 case ISN_LISTINDEX:
2789 case ISN_LISTSLICE:
2790 case ISN_LOAD:
2791 case ISN_LOADBDICT:
2792 case ISN_LOADGDICT:
2793 case ISN_LOADOUTER:
2794 case ISN_LOADREG:
2795 case ISN_LOADTDICT:
2796 case ISN_LOADV:
2797 case ISN_LOADWDICT:
2798 case ISN_LOCKCONST:
2799 case ISN_MEMBER:
2800 case ISN_NEGATENR:
2801 case ISN_NEWDICT:
2802 case ISN_NEWLIST:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002803 case ISN_NEWPARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002804 case ISN_OPANY:
2805 case ISN_OPFLOAT:
2806 case ISN_OPNR:
2807 case ISN_PCALL:
2808 case ISN_PCALL_END:
2809 case ISN_PROF_END:
2810 case ISN_PROF_START:
2811 case ISN_PUSHBOOL:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002812 case ISN_PUSHCHANNEL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002813 case ISN_PUSHF:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002814 case ISN_PUSHJOB:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002815 case ISN_PUSHNR:
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002816 case ISN_PUSHOBJ:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002817 case ISN_PUSHSPEC:
2818 case ISN_PUT:
64-bitmane08f10a2025-03-18 22:14:34 +01002819 case ISN_IPUT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002820 case ISN_REDIREND:
2821 case ISN_REDIRSTART:
2822 case ISN_RETURN:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002823 case ISN_RETURN_OBJECT:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002824 case ISN_RETURN_VOID:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002825 case ISN_SHUFFLE:
2826 case ISN_SLICE:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002827 case ISN_SOURCE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002828 case ISN_STORE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002829 case ISN_STORENR:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002830 case ISN_STOREOUTER:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002831 case ISN_STORE_THIS:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002832 case ISN_STORERANGE:
2833 case ISN_STOREREG:
2834 case ISN_STOREV:
2835 case ISN_STRINDEX:
2836 case ISN_STRSLICE:
2837 case ISN_THROW:
2838 case ISN_TRYCONT:
2839 case ISN_UNLETINDEX:
2840 case ISN_UNLETRANGE:
2841 case ISN_UNPACK:
2842 case ISN_USEDICT:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002843 case ISN_WHILE:
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +01002844 case ISN_SCRIPTCTX_SET:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002845 // nothing allocated
2846 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002847 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002848}
2849
2850 void
2851clear_instr_ga(garray_T *gap)
2852{
2853 int idx;
2854
2855 for (idx = 0; idx < gap->ga_len; ++idx)
2856 delete_instr(((isn_T *)gap->ga_data) + idx);
2857 ga_clear(gap);
2858}
2859
2860
2861#endif // defined(FEAT_EVAL)