blob: ad8beb1a30a232d86d9ccd5e68e036b31d4f060e [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)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001448 extra->fre_func_name = vim_strsave(ufunc->uf_name);
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.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001808 * Return FAIL if the number of arguments is wrong.
1809 */
1810 int
Bram Moolenaard0200c82023-01-28 15:19:40 +00001811generate_CALL(
1812 cctx_T *cctx,
1813 ufunc_T *ufunc,
1814 class_T *cl,
1815 int mi,
1816 int pushed_argcount)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001817{
1818 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001819 int regular_args = ufunc->uf_args.ga_len;
1820 int argcount = pushed_argcount;
1821
1822 RETURN_OK_IF_SKIP(cctx);
1823 if (argcount > regular_args && !has_varargs(ufunc))
1824 {
1825 semsg(_(e_too_many_arguments_for_function_str),
1826 printable_func_name(ufunc));
1827 return FAIL;
1828 }
1829 if (argcount < regular_args - ufunc->uf_def_args.ga_len)
1830 {
1831 semsg(_(e_not_enough_arguments_for_function_str),
1832 printable_func_name(ufunc));
1833 return FAIL;
1834 }
1835
1836 if (ufunc->uf_def_status != UF_NOT_COMPILED
1837 && ufunc->uf_def_status != UF_COMPILE_ERROR)
1838 {
1839 int i;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001840 compiletype_T compile_type;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001841
1842 for (i = 0; i < argcount; ++i)
1843 {
1844 type_T *expected;
1845 type_T *actual;
1846
Bram Moolenaar078a4612022-01-04 15:17:03 +00001847 actual = get_type_on_stack(cctx, argcount - i - 1);
Ernie Raelb077b582023-12-14 20:11:44 +01001848 if (check_type_is_value(actual) == FAIL)
1849 return FAIL;
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00001850 if (actual->tt_type == VAR_SPECIAL
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001851 && i >= regular_args - ufunc->uf_def_args.ga_len)
1852 {
1853 // assume v:none used for default argument value
1854 continue;
1855 }
1856 if (i < regular_args)
1857 {
1858 if (ufunc->uf_arg_types == NULL)
1859 continue;
1860 expected = ufunc->uf_arg_types[i];
1861 }
1862 else if (ufunc->uf_va_type == NULL
1863 || ufunc->uf_va_type == &t_list_any)
1864 // possibly a lambda or "...: any"
1865 expected = &t_any;
1866 else
1867 expected = ufunc->uf_va_type->tt_member;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00001868 if (need_type(actual, expected, FALSE,
1869 -argcount + i, i + 1, cctx, TRUE, FALSE) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001870 {
1871 arg_type_mismatch(expected, actual, i + 1);
1872 return FAIL;
1873 }
1874 }
Bram Moolenaar139575d2022-03-15 19:29:30 +00001875 compile_type = get_compile_type(ufunc);
1876 if (func_needs_compiling(ufunc, compile_type)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001877 && compile_def_function(ufunc, ufunc->uf_ret_type == NULL,
Bram Moolenaar139575d2022-03-15 19:29:30 +00001878 compile_type, NULL) == FAIL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001879 return FAIL;
1880 }
1881 if (ufunc->uf_def_status == UF_COMPILE_ERROR)
1882 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001883 emsg_funcname(e_call_to_function_that_failed_to_compile_str,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001884 ufunc->uf_name);
1885 return FAIL;
1886 }
1887
Bram Moolenaard0200c82023-01-28 15:19:40 +00001888 if ((isn = generate_instr(cctx, cl != NULL ? ISN_METHODCALL
1889 : ufunc->uf_def_status != UF_NOT_COMPILED
1890 ? ISN_DCALL : ISN_UCALL)) == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001891 return FAIL;
Bram Moolenaar836137d2023-01-29 14:11:24 +00001892 if (cl != NULL /* isn->isn_type == ISN_METHODCALL */)
Bram Moolenaard0200c82023-01-28 15:19:40 +00001893 {
1894 isn->isn_arg.mfunc = ALLOC_ONE(cmfunc_T);
1895 if (isn->isn_arg.mfunc == NULL)
1896 return FAIL;
1897 isn->isn_arg.mfunc->cmf_itf = cl;
1898 ++cl->class_refcount;
1899 isn->isn_arg.mfunc->cmf_idx = mi;
1900 isn->isn_arg.mfunc->cmf_argcount = argcount;
1901 }
1902 else if (isn->isn_type == ISN_DCALL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001903 {
1904 isn->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1905 isn->isn_arg.dfunc.cdf_argcount = argcount;
1906 }
1907 else
1908 {
1909 // A user function may be deleted and redefined later, can't use the
1910 // ufunc pointer, need to look it up again at runtime.
1911 isn->isn_arg.ufunc.cuf_name = vim_strsave(ufunc->uf_name);
1912 isn->isn_arg.ufunc.cuf_argcount = argcount;
1913 }
1914
Bram Moolenaar078a4612022-01-04 15:17:03 +00001915 // drop the argument types
1916 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001917
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02001918 // For an object or class method call, drop the object/class type.
Yegappan Lakshmanan639751d2023-08-27 19:23:37 +02001919 if (ufunc->uf_class != NULL)
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02001920 {
1921 // When a class method is called without the class name prefix, then
1922 // the type will not be in the stack.
1923 type_T *stype = get_type_on_stack(cctx, 0);
1924 if (stype->tt_type == VAR_CLASS || stype->tt_type == VAR_OBJECT)
1925 cctx->ctx_type_stack.ga_len--;
1926 }
Yegappan Lakshmanan639751d2023-08-27 19:23:37 +02001927
Bram Moolenaar078a4612022-01-04 15:17:03 +00001928 // add return type
1929 return push_type_stack(cctx, ufunc->uf_ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001930}
1931
1932/*
1933 * Generate an ISN_UCALL instruction when the function isn't defined yet.
1934 */
1935 int
1936generate_UCALL(cctx_T *cctx, char_u *name, int argcount)
1937{
1938 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001939
1940 RETURN_OK_IF_SKIP(cctx);
1941 if ((isn = generate_instr(cctx, ISN_UCALL)) == NULL)
1942 return FAIL;
1943 isn->isn_arg.ufunc.cuf_name = vim_strsave(name);
1944 isn->isn_arg.ufunc.cuf_argcount = argcount;
1945
Bram Moolenaar078a4612022-01-04 15:17:03 +00001946 // drop the argument types
1947 cctx->ctx_type_stack.ga_len -= argcount;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001948
Bram Moolenaar078a4612022-01-04 15:17:03 +00001949 // add return value
1950 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001951}
1952
1953/*
Bram Moolenaar16900322022-09-08 19:51:45 +01001954 * Check the arguments of function "type" against the types on the stack.
1955 * Returns OK or FAIL;
1956 */
1957 int
1958check_func_args_from_type(
1959 cctx_T *cctx,
1960 type_T *type,
1961 int argcount,
1962 int at_top,
1963 char_u *name)
1964{
1965 if (type->tt_argcount != -1)
1966 {
1967 int varargs = (type->tt_flags & TTFLAG_VARARGS) ? 1 : 0;
1968
1969 if (argcount < type->tt_min_argcount - varargs)
1970 {
1971 emsg_funcname(e_not_enough_arguments_for_function_str, name);
1972 return FAIL;
1973 }
1974 if (!varargs && argcount > type->tt_argcount)
1975 {
1976 emsg_funcname(e_too_many_arguments_for_function_str, name);
1977 return FAIL;
1978 }
1979 if (type->tt_args != NULL)
1980 {
1981 int i;
1982
1983 for (i = 0; i < argcount; ++i)
1984 {
1985 int offset = -argcount + i - (at_top ? 0 : 1);
1986 type_T *actual = get_type_on_stack(cctx, -1 - offset);
1987 type_T *expected;
1988
Ernie Raelb077b582023-12-14 20:11:44 +01001989 if (check_type_is_value(actual) == FAIL)
1990 return FAIL;
Bram Moolenaar16900322022-09-08 19:51:45 +01001991 if (varargs && i >= type->tt_argcount - 1)
Bram Moolenaar36818a92023-01-03 12:33:26 +00001992 {
1993 expected = type->tt_args[type->tt_argcount - 1];
1994 if (expected != NULL && expected->tt_type == VAR_LIST)
1995 expected = expected->tt_member;
1996 if (expected == NULL)
1997 expected = &t_any;
1998 }
Bram Moolenaar16900322022-09-08 19:51:45 +01001999 else if (i >= type->tt_min_argcount
2000 && actual->tt_type == VAR_SPECIAL)
2001 expected = &t_any;
2002 else
2003 expected = type->tt_args[i];
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002004 if (need_type(actual, expected, FALSE,
2005 offset, i + 1, cctx, TRUE, FALSE) == FAIL)
Bram Moolenaar16900322022-09-08 19:51:45 +01002006 {
2007 arg_type_mismatch(expected, actual, i + 1);
2008 return FAIL;
2009 }
2010 }
2011 }
2012 }
2013
2014 return OK;
2015}
2016/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002017 * Generate an ISN_PCALL instruction.
2018 * "type" is the type of the FuncRef.
2019 */
2020 int
2021generate_PCALL(
2022 cctx_T *cctx,
2023 int argcount,
2024 char_u *name,
2025 type_T *type,
2026 int at_top)
2027{
2028 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002029 type_T *ret_type;
2030
2031 RETURN_OK_IF_SKIP(cctx);
2032
Bram Moolenaar59618fe2021-12-21 12:32:17 +00002033 if (type->tt_type == VAR_ANY || type->tt_type == VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002034 ret_type = &t_any;
2035 else if (type->tt_type == VAR_FUNC || type->tt_type == VAR_PARTIAL)
2036 {
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002037 if (check_func_args_from_type(cctx, type, argcount, at_top, name)
2038 == FAIL)
Bram Moolenaar16900322022-09-08 19:51:45 +01002039 return FAIL;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002040
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002041 ret_type = type->tt_member;
2042 if (ret_type == &t_unknown)
2043 // return type not known yet, use a runtime check
2044 ret_type = &t_any;
2045 }
2046 else
2047 {
2048 semsg(_(e_not_callable_type_str), name);
2049 return FAIL;
2050 }
2051
2052 if ((isn = generate_instr(cctx, ISN_PCALL)) == NULL)
2053 return FAIL;
2054 isn->isn_arg.pfunc.cpf_top = at_top;
2055 isn->isn_arg.pfunc.cpf_argcount = argcount;
2056
Bram Moolenaar078a4612022-01-04 15:17:03 +00002057 // drop the arguments and the funcref/partial
2058 cctx->ctx_type_stack.ga_len -= argcount + 1;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002059
Bram Moolenaar078a4612022-01-04 15:17:03 +00002060 // push the return value
2061 push_type_stack(cctx, ret_type);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002062
2063 // If partial is above the arguments it must be cleared and replaced with
2064 // the return value.
2065 if (at_top && generate_instr(cctx, ISN_PCALL_END) == NULL)
2066 return FAIL;
2067
2068 return OK;
2069}
2070
2071/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002072 * Generate an ISN_DEFER instruction.
2073 */
2074 int
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02002075generate_DEFER(cctx_T *cctx, int var_idx, int argcount)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002076{
2077 isn_T *isn;
2078
2079 RETURN_OK_IF_SKIP(cctx);
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02002080 if ((isn = generate_instr_drop(cctx, ISN_DEFER, argcount + 1)) == NULL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002081 return FAIL;
2082 isn->isn_arg.defer.defer_var_idx = var_idx;
2083 isn->isn_arg.defer.defer_argcount = argcount;
2084 return OK;
2085}
2086
2087/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002088 * Generate an ISN_STRINGMEMBER instruction.
2089 */
2090 int
2091generate_STRINGMEMBER(cctx_T *cctx, char_u *name, size_t len)
2092{
2093 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002094 type_T *type;
2095
2096 RETURN_OK_IF_SKIP(cctx);
2097 if ((isn = generate_instr(cctx, ISN_STRINGMEMBER)) == NULL)
2098 return FAIL;
2099 isn->isn_arg.string = vim_strnsave(name, len);
2100
2101 // check for dict type
Bram Moolenaar078a4612022-01-04 15:17:03 +00002102 type = get_type_on_stack(cctx, 0);
Bram Moolenaar0089ce22022-10-08 14:39:36 +01002103 if (type->tt_type != VAR_DICT
2104 && type->tt_type != VAR_ANY && type->tt_type != VAR_UNKNOWN)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002105 {
2106 char *tofree;
2107
2108 semsg(_(e_expected_dictionary_for_using_key_str_but_got_str),
2109 name, type_name(type, &tofree));
2110 vim_free(tofree);
2111 return FAIL;
2112 }
2113 // change dict type to dict member type
2114 if (type->tt_type == VAR_DICT)
2115 {
Bram Moolenaar0089ce22022-10-08 14:39:36 +01002116 type_T *ntype = type->tt_member->tt_type == VAR_UNKNOWN
Bram Moolenaar078a4612022-01-04 15:17:03 +00002117 ? &t_any : type->tt_member;
2118 set_type_on_stack(cctx, ntype, 0);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002119 }
2120
2121 return OK;
2122}
2123
2124/*
2125 * Generate an ISN_ECHO instruction.
2126 */
2127 int
2128generate_ECHO(cctx_T *cctx, int with_white, int count)
2129{
2130 isn_T *isn;
2131
2132 RETURN_OK_IF_SKIP(cctx);
2133 if ((isn = generate_instr_drop(cctx, ISN_ECHO, count)) == NULL)
2134 return FAIL;
2135 isn->isn_arg.echo.echo_with_white = with_white;
2136 isn->isn_arg.echo.echo_count = count;
2137
2138 return OK;
2139}
2140
2141/*
2142 * Generate an ISN_EXECUTE/ISN_ECHOMSG/ISN_ECHOERR instruction.
2143 */
2144 int
2145generate_MULT_EXPR(cctx_T *cctx, isntype_T isn_type, int count)
2146{
2147 isn_T *isn;
2148
Bram Moolenaar2eae3d22022-10-07 15:09:27 +01002149 RETURN_OK_IF_SKIP(cctx);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002150 if ((isn = generate_instr_drop(cctx, isn_type, count)) == NULL)
2151 return FAIL;
2152 isn->isn_arg.number = count;
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002153 return OK;
2154}
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002155
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01002156/*
2157 * Generate an ISN_ECHOWINDOW instruction
2158 */
2159 int
2160generate_ECHOWINDOW(cctx_T *cctx, int count, long time)
2161{
2162 isn_T *isn;
2163
2164 if ((isn = generate_instr_drop(cctx, ISN_ECHOWINDOW, count)) == NULL)
2165 return FAIL;
2166 isn->isn_arg.echowin.ewin_count = count;
2167 isn->isn_arg.echowin.ewin_time = time;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002168 return OK;
2169}
2170
2171/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002172 * Generate an ISN_SOURCE instruction.
2173 */
2174 int
2175generate_SOURCE(cctx_T *cctx, int sid)
2176{
2177 isn_T *isn;
2178
2179 if ((isn = generate_instr(cctx, ISN_SOURCE)) == NULL)
2180 return FAIL;
2181 isn->isn_arg.number = sid;
2182
2183 return OK;
2184}
2185
2186/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002187 * Generate an ISN_PUT instruction.
2188 */
2189 int
2190generate_PUT(cctx_T *cctx, int regname, linenr_T lnum)
2191{
2192 isn_T *isn;
2193
2194 RETURN_OK_IF_SKIP(cctx);
2195 if ((isn = generate_instr(cctx, ISN_PUT)) == NULL)
2196 return FAIL;
2197 isn->isn_arg.put.put_regname = regname;
2198 isn->isn_arg.put.put_lnum = lnum;
2199 return OK;
2200}
2201
2202/*
Ernie Rael64885642023-10-04 20:16:22 +02002203 * Generate a LOCKUNLOCK instruction.The root item, where the indexing starts
2204 * to find the variable, is on the stack. The instr takes
2205 * - the string to parse, "root.b[idx1][idx2].d.val", to find the variable
2206 * - the class, if any, in which the string executes.
2207 * - if the root item is a function argument
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002208 * A copy is made of "line".
2209 */
2210 int
Ernie Raelee865f32023-09-29 19:53:55 +02002211generate_LOCKUNLOCK(cctx_T *cctx, char_u *line, int is_arg)
2212{
2213 isn_T *isn;
2214
2215 RETURN_OK_IF_SKIP(cctx);
2216 if ((isn = generate_instr(cctx, ISN_LOCKUNLOCK)) == NULL)
2217 return FAIL;
Ernie Rael64885642023-10-04 20:16:22 +02002218 class_T *cl = cctx->ctx_ufunc != NULL ? cctx->ctx_ufunc->uf_class : NULL;
2219 isn->isn_arg.lockunlock.lu_string = vim_strsave(line);
2220 isn->isn_arg.lockunlock.lu_cl_exec = cl;
2221 if (cl != NULL)
2222 ++cl->class_refcount;
2223 isn->isn_arg.lockunlock.lu_is_arg = is_arg;
Ernie Raelee865f32023-09-29 19:53:55 +02002224 return OK;
2225}
2226
2227/*
2228 * Generate an EXEC instruction that takes a string argument.
2229 * A copy is made of "line".
2230 */
2231 int
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002232generate_EXEC_copy(cctx_T *cctx, isntype_T isntype, char_u *line)
2233{
2234 isn_T *isn;
2235
2236 RETURN_OK_IF_SKIP(cctx);
2237 if ((isn = generate_instr(cctx, isntype)) == NULL)
2238 return FAIL;
2239 isn->isn_arg.string = vim_strsave(line);
2240 return OK;
2241}
2242
2243/*
2244 * Generate an EXEC instruction that takes a string argument.
2245 * "str" must be allocated, it is consumed.
2246 */
2247 int
2248generate_EXEC(cctx_T *cctx, isntype_T isntype, char_u *str)
2249{
2250 isn_T *isn;
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002251 int ret = OK;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002252
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002253 if (cctx->ctx_skip != SKIP_YES)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002254 {
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002255 if ((isn = generate_instr(cctx, isntype)) == NULL)
2256 ret = FAIL;
2257 else
2258 {
2259 isn->isn_arg.string = str;
2260 return OK;
2261 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002262 }
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01002263 vim_free(str);
2264 return ret;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002265}
2266
2267 int
2268generate_LEGACY_EVAL(cctx_T *cctx, char_u *line)
2269{
2270 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002271
2272 RETURN_OK_IF_SKIP(cctx);
2273 if ((isn = generate_instr(cctx, ISN_LEGACY_EVAL)) == NULL)
2274 return FAIL;
2275 isn->isn_arg.string = vim_strsave(line);
2276
Bram Moolenaar078a4612022-01-04 15:17:03 +00002277 return push_type_stack(cctx, &t_any);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002278}
2279
2280 int
2281generate_EXECCONCAT(cctx_T *cctx, int count)
2282{
2283 isn_T *isn;
2284
2285 if ((isn = generate_instr_drop(cctx, ISN_EXECCONCAT, count)) == NULL)
2286 return FAIL;
2287 isn->isn_arg.number = count;
2288 return OK;
2289}
2290
2291/*
2292 * Generate ISN_RANGE. Consumes "range". Return OK/FAIL.
2293 */
2294 int
2295generate_RANGE(cctx_T *cctx, char_u *range)
2296{
2297 isn_T *isn;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002298
2299 if ((isn = generate_instr(cctx, ISN_RANGE)) == NULL)
2300 return FAIL;
2301 isn->isn_arg.string = range;
2302
Bram Moolenaar078a4612022-01-04 15:17:03 +00002303 return push_type_stack(cctx, &t_number);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002304}
2305
2306 int
2307generate_UNPACK(cctx_T *cctx, int var_count, int semicolon)
2308{
2309 isn_T *isn;
2310
2311 RETURN_OK_IF_SKIP(cctx);
2312 if ((isn = generate_instr(cctx, ISN_UNPACK)) == NULL)
2313 return FAIL;
2314 isn->isn_arg.unpack.unp_count = var_count;
2315 isn->isn_arg.unpack.unp_semicolon = semicolon;
2316 return OK;
2317}
2318
2319/*
2320 * Generate an instruction for any command modifiers.
2321 */
2322 int
2323generate_cmdmods(cctx_T *cctx, cmdmod_T *cmod)
2324{
2325 isn_T *isn;
2326
2327 if (has_cmdmod(cmod, FALSE))
2328 {
2329 cctx->ctx_has_cmdmod = TRUE;
2330
2331 if ((isn = generate_instr(cctx, ISN_CMDMOD)) == NULL)
2332 return FAIL;
2333 isn->isn_arg.cmdmod.cf_cmdmod = ALLOC_ONE(cmdmod_T);
2334 if (isn->isn_arg.cmdmod.cf_cmdmod == NULL)
2335 return FAIL;
2336 mch_memmove(isn->isn_arg.cmdmod.cf_cmdmod, cmod, sizeof(cmdmod_T));
2337 // filter program now belongs to the instruction
2338 cmod->cmod_filter_regmatch.regprog = NULL;
2339 }
2340
2341 return OK;
2342}
2343
2344 int
2345generate_undo_cmdmods(cctx_T *cctx)
2346{
2347 if (cctx->ctx_has_cmdmod && generate_instr(cctx, ISN_CMDMOD_REV) == NULL)
2348 return FAIL;
2349 cctx->ctx_has_cmdmod = FALSE;
2350 return OK;
2351}
2352
2353/*
2354 * Generate a STORE instruction for "dest", not being "dest_local".
Bram Moolenaard505d172022-12-18 21:42:55 +00002355 * "lhs" might be NULL.
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002356 * Return FAIL when out of memory.
2357 */
2358 int
2359generate_store_var(
2360 cctx_T *cctx,
2361 assign_dest_T dest,
2362 int opt_flags,
2363 int vimvaridx,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002364 type_T *type,
Bram Moolenaard505d172022-12-18 21:42:55 +00002365 char_u *name,
2366 lhs_T *lhs)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002367{
2368 switch (dest)
2369 {
2370 case dest_option:
2371 return generate_STOREOPT(cctx, ISN_STOREOPT,
2372 skip_option_env_lead(name), opt_flags);
2373 case dest_func_option:
2374 return generate_STOREOPT(cctx, ISN_STOREFUNCOPT,
2375 skip_option_env_lead(name), opt_flags);
2376 case dest_global:
2377 // include g: with the name, easier to execute that way
2378 return generate_STORE(cctx, vim_strchr(name, AUTOLOAD_CHAR) == NULL
2379 ? ISN_STOREG : ISN_STOREAUTO, 0, name);
2380 case dest_buffer:
2381 // include b: with the name, easier to execute that way
2382 return generate_STORE(cctx, ISN_STOREB, 0, name);
2383 case dest_window:
2384 // include w: with the name, easier to execute that way
2385 return generate_STORE(cctx, ISN_STOREW, 0, name);
2386 case dest_tab:
2387 // include t: with the name, easier to execute that way
2388 return generate_STORE(cctx, ISN_STORET, 0, name);
2389 case dest_env:
2390 return generate_STORE(cctx, ISN_STOREENV, 0, name + 1);
2391 case dest_reg:
2392 return generate_STORE(cctx, ISN_STOREREG,
2393 name[1] == '@' ? '"' : name[1], NULL);
2394 case dest_vimvar:
2395 return generate_STORE(cctx, ISN_STOREV, vimvaridx, NULL);
2396 case dest_script:
Ernie Rael3f821d62024-04-24 20:07:50 +02002397 case dest_script_v9:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002398 {
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002399 int scriptvar_idx = lhs->lhs_scriptvar_idx;
2400 int scriptvar_sid = lhs->lhs_scriptvar_sid;
2401 if (scriptvar_idx < 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002402 {
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002403 isntype_T isn_type = ISN_STORES;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002404
Ernie Rael3f821d62024-04-24 20:07:50 +02002405 // If "sn_import_autoload", generate ISN_STOREEXPORT (not
2406 // ISN_STORES) if destination is in a vim9script or if
2407 // there is no "sn_autoload_prefix".
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002408 if (SCRIPT_ID_VALID(scriptvar_sid)
2409 && SCRIPT_ITEM(scriptvar_sid)->sn_import_autoload
Ernie Rael3f821d62024-04-24 20:07:50 +02002410 && ((SCRIPT_ITEM(scriptvar_sid)
2411 ->sn_autoload_prefix == NULL)
2412 || lhs->lhs_dest == dest_script_v9))
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002413 {
2414 // "import autoload './dir/script.vim'" - load script
2415 // first
2416 if (generate_SOURCE(cctx, scriptvar_sid) == FAIL)
2417 return FAIL;
2418 isn_type = ISN_STOREEXPORT;
2419 }
2420
2421 // "s:" may be included in the name.
2422 return generate_OLDSCRIPT(cctx, isn_type, name,
2423 scriptvar_sid, type);
2424 }
2425 return generate_VIM9SCRIPT(cctx, ISN_STORESCRIPT,
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002426 scriptvar_sid, scriptvar_idx, type);
Bram Moolenaarc336ae32022-12-18 22:01:42 +00002427 }
Bram Moolenaard505d172022-12-18 21:42:55 +00002428 case dest_class_member:
2429 return generate_CLASSMEMBER(cctx, FALSE,
2430 lhs->lhs_class, lhs->lhs_classmember_idx);
2431
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002432 case dest_local:
2433 case dest_expr:
2434 // cannot happen
2435 break;
2436 }
2437 return FAIL;
2438}
2439
Bram Moolenaar38ecd972022-01-15 21:44:44 +00002440/*
2441 * Return TRUE when inside a "for" or "while" loop.
2442 */
2443 int
2444inside_loop_scope(cctx_T *cctx)
2445{
2446 scope_T *scope = cctx->ctx_scope;
2447
2448 for (;;)
2449 {
2450 if (scope == NULL)
2451 break;
2452 if (scope->se_type == FOR_SCOPE || scope->se_type == WHILE_SCOPE)
2453 return TRUE;
2454 scope = scope->se_outer;
2455 }
2456 return FALSE;
2457}
2458
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002459 int
Bram Moolenaar5cd64792021-12-25 18:23:24 +00002460generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002461{
2462 if (lhs->lhs_dest != dest_local)
2463 return generate_store_var(cctx, lhs->lhs_dest,
2464 lhs->lhs_opt_flags, lhs->lhs_vimvaridx,
Bram Moolenaard505d172022-12-18 21:42:55 +00002465 lhs->lhs_type, lhs->lhs_name, lhs);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002466
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002467 if (lhs->lhs_lvar == NULL)
2468 return OK;
2469
2470 garray_T *instr = &cctx->ctx_instr;
2471 isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
2472
2473 // Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
2474 // ISN_STORENR.
2475 // And "var = 0" does not need any instruction.
2476 if (lhs->lhs_lvar->lv_from_outer == 0
2477 && instr->ga_len == instr_count + 1
2478 && isn->isn_type == ISN_PUSHNR)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002479 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002480 varnumber_T val = isn->isn_arg.number;
2481 garray_T *stack = &cctx->ctx_type_stack;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002482
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002483 if (val == 0 && is_decl && !inside_loop_scope(cctx))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002484 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002485 // zero is the default value, no need to do anything
2486 --instr->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002487 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002488 else
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002489 {
2490 isn->isn_type = ISN_STORENR;
2491 isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
2492 isn->isn_arg.storenr.stnr_val = val;
2493 }
2494 if (stack->ga_len > 0)
2495 --stack->ga_len;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002496 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002497 else if (lhs->lhs_lvar->lv_from_outer > 0)
2498 generate_STOREOUTER(cctx, lhs->lhs_lvar->lv_idx,
2499 lhs->lhs_lvar->lv_from_outer, lhs->lhs_lvar->lv_loop_idx);
2500 else
2501 generate_STORE(cctx, ISN_STORE, lhs->lhs_lvar->lv_idx, NULL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002502 return OK;
2503}
2504
2505#if defined(FEAT_PROFILE) || defined(PROTO)
2506 void
2507may_generate_prof_end(cctx_T *cctx, int prof_lnum)
2508{
2509 if (cctx->ctx_compile_type == CT_PROFILE && prof_lnum >= 0)
2510 generate_instr(cctx, ISN_PROF_END);
2511}
2512#endif
2513
2514
2515/*
2516 * Delete an instruction, free what it contains.
2517 */
2518 void
2519delete_instr(isn_T *isn)
2520{
2521 switch (isn->isn_type)
2522 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002523 case ISN_AUTOLOAD:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002524 case ISN_DEF:
2525 case ISN_EXEC:
2526 case ISN_EXECRANGE:
2527 case ISN_EXEC_SPLIT:
2528 case ISN_LEGACY_EVAL:
2529 case ISN_LOADAUTO:
2530 case ISN_LOADB:
2531 case ISN_LOADENV:
2532 case ISN_LOADG:
2533 case ISN_LOADOPT:
2534 case ISN_LOADT:
2535 case ISN_LOADW:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002536 case ISN_PUSHEXC:
2537 case ISN_PUSHFUNC:
2538 case ISN_PUSHS:
2539 case ISN_RANGE:
2540 case ISN_STOREAUTO:
2541 case ISN_STOREB:
2542 case ISN_STOREENV:
2543 case ISN_STOREG:
2544 case ISN_STORET:
2545 case ISN_STOREW:
2546 case ISN_STRINGMEMBER:
2547 vim_free(isn->isn_arg.string);
2548 break;
2549
Ernie Rael64885642023-10-04 20:16:22 +02002550 case ISN_LOCKUNLOCK:
2551 class_unref(isn->isn_arg.lockunlock.lu_cl_exec);
2552 vim_free(isn->isn_arg.lockunlock.lu_string);
2553 break;
2554
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002555 case ISN_SUBSTITUTE:
2556 {
2557 int idx;
2558 isn_T *list = isn->isn_arg.subs.subs_instr;
2559
2560 vim_free(isn->isn_arg.subs.subs_cmd);
2561 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2562 delete_instr(list + idx);
2563 vim_free(list);
2564 }
2565 break;
2566
2567 case ISN_INSTR:
2568 {
2569 int idx;
2570 isn_T *list = isn->isn_arg.instr;
2571
2572 for (idx = 0; list[idx].isn_type != ISN_FINISH; ++idx)
2573 delete_instr(list + idx);
2574 vim_free(list);
2575 }
2576 break;
2577
2578 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002579 case ISN_LOADEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002580 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002581 case ISN_STOREEXPORT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002582 vim_free(isn->isn_arg.loadstore.ls_name);
2583 break;
2584
2585 case ISN_UNLET:
2586 case ISN_UNLETENV:
2587 vim_free(isn->isn_arg.unlet.ul_name);
2588 break;
2589
2590 case ISN_STOREOPT:
2591 case ISN_STOREFUNCOPT:
2592 vim_free(isn->isn_arg.storeopt.so_name);
2593 break;
2594
2595 case ISN_PUSHBLOB: // push blob isn_arg.blob
2596 blob_unref(isn->isn_arg.blob);
2597 break;
2598
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002599 case ISN_PUSHCLASS:
Bram Moolenaar30a84472023-02-27 08:07:14 +00002600 class_unref(isn->isn_arg.classarg);
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002601 break;
2602
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002603 case ISN_UCALL:
2604 vim_free(isn->isn_arg.ufunc.cuf_name);
2605 break;
2606
2607 case ISN_FUNCREF:
2608 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002609 funcref_T *funcref = &isn->isn_arg.funcref;
2610 funcref_extra_T *extra = funcref->fr_extra;
2611
2612 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002613 {
2614 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002615 + funcref->fr_dfunc_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002616 ufunc_T *ufunc = dfunc->df_ufunc;
2617
2618 if (ufunc != NULL && func_name_refcount(ufunc->uf_name))
2619 func_ptr_unref(ufunc);
2620 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002621 if (extra != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002622 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002623 char_u *name = extra->fre_func_name;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002624
2625 if (name != NULL)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002626 {
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002627 func_unref(name);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002628 vim_free(name);
2629 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00002630 if (extra->fre_class != NULL)
2631 class_unref(extra->fre_class);
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002632 vim_free(extra);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002633 }
2634 }
2635 break;
2636
2637 case ISN_DCALL:
2638 {
2639 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002640 + isn->isn_arg.dfunc.cdf_idx;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002641
2642 if (dfunc->df_ufunc != NULL
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002643 && func_name_refcount(dfunc->df_ufunc->uf_name))
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002644 func_ptr_unref(dfunc->df_ufunc);
2645 }
2646 break;
2647
Bram Moolenaard0200c82023-01-28 15:19:40 +00002648 case ISN_METHODCALL:
2649 {
2650 cmfunc_T *mfunc = isn->isn_arg.mfunc;
2651 class_unref(mfunc->cmf_itf);
2652 vim_free(mfunc);
2653 }
2654 break;
2655
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002656 case ISN_NEWFUNC:
2657 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002658 newfuncarg_T *arg = isn->isn_arg.newfunc.nf_arg;
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002659
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002660 if (arg != NULL)
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002661 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002662 ufunc_T *ufunc = find_func_even_dead(
2663 arg->nfa_lambda, FFED_IS_GLOBAL);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002664
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002665 if (ufunc != NULL)
2666 {
2667 unlink_def_function(ufunc);
2668 func_ptr_unref(ufunc);
2669 }
2670
2671 vim_free(arg->nfa_lambda);
2672 vim_free(arg->nfa_global);
2673 vim_free(arg);
2674 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002675 }
2676 break;
2677
2678 case ISN_CHECKTYPE:
2679 case ISN_SETTYPE:
2680 free_type(isn->isn_arg.type.ct_type);
2681 break;
2682
2683 case ISN_CMDMOD:
2684 vim_regfree(isn->isn_arg.cmdmod.cf_cmdmod
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002685 ->cmod_filter_regmatch.regprog);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002686 vim_free(isn->isn_arg.cmdmod.cf_cmdmod);
2687 break;
2688
2689 case ISN_LOADSCRIPT:
2690 case ISN_STORESCRIPT:
2691 vim_free(isn->isn_arg.script.scriptref);
2692 break;
2693
Bram Moolenaard505d172022-12-18 21:42:55 +00002694 case ISN_LOAD_CLASSMEMBER:
2695 case ISN_STORE_CLASSMEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00002696 case ISN_GET_ITF_MEMBER:
Bram Moolenaard505d172022-12-18 21:42:55 +00002697 class_unref(isn->isn_arg.classmember.cm_class);
2698 break;
2699
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002700 case ISN_STOREINDEX:
2701 class_unref(isn->isn_arg.storeindex.si_class);
2702 break;
2703
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002704 case ISN_TRY:
Bram Moolenaar0d807102021-12-21 09:42:09 +00002705 vim_free(isn->isn_arg.tryref.try_ref);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002706 break;
2707
2708 case ISN_CEXPR_CORE:
2709 vim_free(isn->isn_arg.cexpr.cexpr_ref->cer_cmdline);
2710 vim_free(isn->isn_arg.cexpr.cexpr_ref);
2711 break;
2712
2713 case ISN_2BOOL:
2714 case ISN_2STRING:
2715 case ISN_2STRING_ANY:
2716 case ISN_ADDBLOB:
2717 case ISN_ADDLIST:
2718 case ISN_ANYINDEX:
2719 case ISN_ANYSLICE:
2720 case ISN_BCALL:
2721 case ISN_BLOBAPPEND:
2722 case ISN_BLOBINDEX:
2723 case ISN_BLOBSLICE:
2724 case ISN_CATCH:
2725 case ISN_CEXPR_AUCMD:
2726 case ISN_CHECKLEN:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002727 case ISN_CLEARDICT:
2728 case ISN_CMDMOD_REV:
2729 case ISN_COMPAREANY:
2730 case ISN_COMPAREBLOB:
2731 case ISN_COMPAREBOOL:
2732 case ISN_COMPAREDICT:
2733 case ISN_COMPAREFLOAT:
2734 case ISN_COMPAREFUNC:
2735 case ISN_COMPARELIST:
2736 case ISN_COMPARENR:
Bram Moolenaar7a222242022-03-01 19:23:24 +00002737 case ISN_COMPARENULL:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00002738 case ISN_COMPAREOBJECT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002739 case ISN_COMPARESPECIAL:
2740 case ISN_COMPARESTRING:
2741 case ISN_CONCAT:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002742 case ISN_CONSTRUCT:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002743 case ISN_COND2BOOL:
2744 case ISN_DEBUG:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002745 case ISN_DEFER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002746 case ISN_DROP:
2747 case ISN_ECHO:
2748 case ISN_ECHOCONSOLE:
2749 case ISN_ECHOERR:
2750 case ISN_ECHOMSG:
Bram Moolenaar68a635a2022-09-01 17:26:17 +01002751 case ISN_ECHOWINDOW:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002752 case ISN_ENDLOOP:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002753 case ISN_ENDTRY:
2754 case ISN_EXECCONCAT:
2755 case ISN_EXECUTE:
2756 case ISN_FINALLY:
2757 case ISN_FINISH:
2758 case ISN_FOR:
2759 case ISN_GETITEM:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002760 case ISN_GET_OBJ_MEMBER:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002761 case ISN_JUMP:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002762 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002763 case ISN_JUMP_IF_ARG_SET:
2764 case ISN_LISTAPPEND:
2765 case ISN_LISTINDEX:
2766 case ISN_LISTSLICE:
2767 case ISN_LOAD:
2768 case ISN_LOADBDICT:
2769 case ISN_LOADGDICT:
2770 case ISN_LOADOUTER:
2771 case ISN_LOADREG:
2772 case ISN_LOADTDICT:
2773 case ISN_LOADV:
2774 case ISN_LOADWDICT:
2775 case ISN_LOCKCONST:
2776 case ISN_MEMBER:
2777 case ISN_NEGATENR:
2778 case ISN_NEWDICT:
2779 case ISN_NEWLIST:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00002780 case ISN_NEWPARTIAL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002781 case ISN_OPANY:
2782 case ISN_OPFLOAT:
2783 case ISN_OPNR:
2784 case ISN_PCALL:
2785 case ISN_PCALL_END:
2786 case ISN_PROF_END:
2787 case ISN_PROF_START:
2788 case ISN_PUSHBOOL:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002789 case ISN_PUSHCHANNEL:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002790 case ISN_PUSHF:
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002791 case ISN_PUSHJOB:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002792 case ISN_PUSHNR:
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002793 case ISN_PUSHOBJ:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002794 case ISN_PUSHSPEC:
2795 case ISN_PUT:
2796 case ISN_REDIREND:
2797 case ISN_REDIRSTART:
2798 case ISN_RETURN:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002799 case ISN_RETURN_OBJECT:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002800 case ISN_RETURN_VOID:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002801 case ISN_SHUFFLE:
2802 case ISN_SLICE:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002803 case ISN_SOURCE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002804 case ISN_STORE:
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002805 case ISN_STORENR:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002806 case ISN_STOREOUTER:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002807 case ISN_STORE_THIS:
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002808 case ISN_STORERANGE:
2809 case ISN_STOREREG:
2810 case ISN_STOREV:
2811 case ISN_STRINDEX:
2812 case ISN_STRSLICE:
2813 case ISN_THROW:
2814 case ISN_TRYCONT:
2815 case ISN_UNLETINDEX:
2816 case ISN_UNLETRANGE:
2817 case ISN_UNPACK:
2818 case ISN_USEDICT:
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002819 case ISN_WHILE:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002820 // nothing allocated
2821 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002822 }
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002823}
2824
2825 void
2826clear_instr_ga(garray_T *gap)
2827{
2828 int idx;
2829
2830 for (idx = 0; idx < gap->ga_len; ++idx)
2831 delete_instr(((isn_T *)gap->ga_data) + idx);
2832 ga_clear(gap);
2833}
2834
2835
2836#endif // defined(FEAT_EVAL)