blob: a7c412e51d1f52c8e32f0e2e02b38d8632c88f8d [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* 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/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +000011 * vim9compile.c: compiling a :def function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010012 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaardc7c3662021-12-20 15:04:29 +000019// When not generating protos this is included in proto.h
20#ifdef PROTO
21# include "vim9.h"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010022#endif
23
Bram Moolenaardc7c3662021-12-20 15:04:29 +000024// Functions defined with :def are stored in this growarray.
25// They are never removed, so that they can be found by index.
26// Deleted functions have the df_deleted flag set.
27garray_T def_functions = {0, 0, sizeof(dfunc_T), 50, NULL};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010028
Bram Moolenaarcdc40c42020-12-26 17:43:08 +010029static void delete_def_function_contents(dfunc_T *dfunc, int mark_deleted);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010030
31/*
Bram Moolenaar709664c2020-12-12 14:33:41 +010032 * Lookup variable "name" in the local scope and return it in "lvar".
Bram Moolenaarab360522021-01-10 14:02:28 +010033 * "lvar->lv_from_outer" is incremented accordingly.
Bram Moolenaar709664c2020-12-12 14:33:41 +010034 * If "lvar" is NULL only check if the variable can be found.
35 * Return FAIL if not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010036 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +000037 int
Bram Moolenaar709664c2020-12-12 14:33:41 +010038lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010039{
40 int idx;
Bram Moolenaar709664c2020-12-12 14:33:41 +010041 lvar_T *lvp;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010042
Bram Moolenaarae8d2de2020-02-13 21:42:24 +010043 if (len == 0)
Bram Moolenaar709664c2020-12-12 14:33:41 +010044 return FAIL;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020045
Bram Moolenaar58b40092023-01-11 15:59:05 +000046 if (((len == 4 && STRNCMP(name, "this", 4) == 0)
47 || (len == 5 && STRNCMP(name, "super", 5) == 0))
Bram Moolenaar00b28d62022-12-08 15:32:33 +000048 && cctx->ctx_ufunc != NULL
Bram Moolenaar574950d2023-01-03 19:08:50 +000049 && (cctx->ctx_ufunc->uf_flags & (FC_OBJECT|FC_NEW)))
Bram Moolenaar00b28d62022-12-08 15:32:33 +000050 {
Bram Moolenaar58b40092023-01-11 15:59:05 +000051 int is_super = *name == 's';
Bram Moolenaar6aa09372023-01-11 17:59:38 +000052 if (is_super)
53 {
54 if (name[5] != '.')
55 {
56 emsg(_(e_super_must_be_followed_by_dot));
57 return FAIL;
58 }
59 if (cctx->ctx_ufunc->uf_class != NULL
60 && cctx->ctx_ufunc->uf_class->class_extends == NULL)
61 {
62 emsg(_(e_using_super_not_in_child_class));
63 return FAIL;
64 }
65 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +000066 if (lvar != NULL)
67 {
68 CLEAR_POINTER(lvar);
Bram Moolenaar486fc252023-01-18 14:51:07 +000069 lvar->lv_loop_depth = -1;
Bram Moolenaar58b40092023-01-11 15:59:05 +000070 lvar->lv_name = (char_u *)(is_super ? "super" : "this");
Bram Moolenaar00b28d62022-12-08 15:32:33 +000071 if (cctx->ctx_ufunc->uf_class != NULL)
Bram Moolenaar58b40092023-01-11 15:59:05 +000072 {
Bram Moolenaarffdaca92022-12-09 21:41:48 +000073 lvar->lv_type = &cctx->ctx_ufunc->uf_class->class_object_type;
Bram Moolenaar58b40092023-01-11 15:59:05 +000074 if (is_super)
75 {
76 type_T *type = get_type_ptr(cctx->ctx_type_list);
77
78 if (type != NULL)
79 {
80 *type = *lvar->lv_type;
81 lvar->lv_type = type;
82 type->tt_flags |= TTFLAG_SUPER;
83 }
84 }
85 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +000086 }
87 return OK;
88 }
89
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020090 // Find local in current function scope.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010091 for (idx = 0; idx < cctx->ctx_locals.ga_len; ++idx)
92 {
Bram Moolenaar709664c2020-12-12 14:33:41 +010093 lvp = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
Bram Moolenaara275f2c2022-10-11 20:04:09 +010094 if (lvp->lv_name != NULL
95 && STRNCMP(name, lvp->lv_name, len) == 0
Bram Moolenaar709664c2020-12-12 14:33:41 +010096 && STRLEN(lvp->lv_name) == len)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020097 {
Bram Moolenaar709664c2020-12-12 14:33:41 +010098 if (lvar != NULL)
99 {
100 *lvar = *lvp;
Bram Moolenaarab360522021-01-10 14:02:28 +0100101 lvar->lv_from_outer = 0;
Bram Moolenaar65449bd2022-09-19 16:02:43 +0100102 // If the variable was declared inside a loop set
103 // lvar->lv_loop_idx and lvar->lv_loop_depth.
104 get_loop_var_idx(cctx, idx, lvar);
Bram Moolenaar709664c2020-12-12 14:33:41 +0100105 }
106 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200107 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100108 }
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200109
110 // Find local in outer function scope.
111 if (cctx->ctx_outer != NULL)
112 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100113 if (lookup_local(name, len, lvar, cctx->ctx_outer) == OK)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200114 {
Bram Moolenaar709664c2020-12-12 14:33:41 +0100115 if (lvar != NULL)
116 {
117 cctx->ctx_outer_used = TRUE;
Bram Moolenaarab360522021-01-10 14:02:28 +0100118 ++lvar->lv_from_outer;
Bram Moolenaar709664c2020-12-12 14:33:41 +0100119 }
120 return OK;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200121 }
122 }
123
Bram Moolenaar709664c2020-12-12 14:33:41 +0100124 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100125}
126
127/*
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200128 * Lookup an argument in the current function and an enclosing function.
129 * Returns the argument index in "idxp"
130 * Returns the argument type in "type"
131 * Sets "gen_load_outer" to TRUE if found in outer scope.
132 * Returns OK when found, FAIL otherwise.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100133 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000134 int
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200135arg_exists(
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200136 char_u *name,
137 size_t len,
138 int *idxp,
139 type_T **type,
140 int *gen_load_outer,
141 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142{
143 int idx;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200144 char_u *va_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100145
Bram Moolenaarae8d2de2020-02-13 21:42:24 +0100146 if (len == 0)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200147 return FAIL;
Bram Moolenaare28d9b32021-07-03 18:56:53 +0200148 for (idx = 0; idx < cctx->ctx_ufunc->uf_args_visible; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149 {
150 char_u *arg = FUNCARG(cctx->ctx_ufunc, idx);
151
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200152 if (STRNCMP(name, arg, len) == 0 && arg[len] == NUL)
153 {
154 if (idxp != NULL)
155 {
156 // Arguments are located above the frame pointer. One further
157 // if there is a vararg argument
158 *idxp = idx - (cctx->ctx_ufunc->uf_args.ga_len
159 + STACK_FRAME_SIZE)
160 + (cctx->ctx_ufunc->uf_va_name != NULL ? -1 : 0);
161
162 if (cctx->ctx_ufunc->uf_arg_types != NULL)
163 *type = cctx->ctx_ufunc->uf_arg_types[idx];
164 else
165 *type = &t_any;
166 }
167 return OK;
168 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100169 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100170
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200171 va_name = cctx->ctx_ufunc->uf_va_name;
172 if (va_name != NULL
173 && STRNCMP(name, va_name, len) == 0 && va_name[len] == NUL)
174 {
175 if (idxp != NULL)
176 {
177 // varargs is always the last argument
178 *idxp = -STACK_FRAME_SIZE - 1;
179 *type = cctx->ctx_ufunc->uf_va_type;
180 }
181 return OK;
182 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100183
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200184 if (cctx->ctx_outer != NULL)
185 {
186 // Lookup the name for an argument of the outer function.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200187 if (arg_exists(name, len, idxp, type, gen_load_outer, cctx->ctx_outer)
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200188 == OK)
189 {
Bram Moolenaar44ec21c2021-02-12 21:50:57 +0100190 if (gen_load_outer != NULL)
191 ++*gen_load_outer;
Bram Moolenaar2fd4cd72020-05-03 22:30:49 +0200192 return OK;
193 }
194 }
195
196 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100197}
198
199/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200200 * Lookup a script-local variable in the current script, possibly defined in a
201 * block that contains the function "cctx->ctx_ufunc".
Bram Moolenaar7a3b8022022-02-14 19:53:03 +0000202 * "cctx" is NULL at the script level, "cstack" is NULL in a function.
Bram Moolenaar18062fc2021-03-05 21:35:47 +0100203 * If "len" is <= 0 "name" must be NUL terminated.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200204 * Return NULL when not found.
205 */
206 static sallvar_T *
Bram Moolenaardce24412022-02-08 20:35:30 +0000207find_script_var(char_u *name, size_t len, cctx_T *cctx, cstack_T *cstack)
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200208{
209 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
210 hashitem_T *hi;
211 int cc;
212 sallvar_T *sav;
213 ufunc_T *ufunc;
214
215 // Find the list of all script variables with the right name.
216 if (len > 0)
217 {
218 cc = name[len];
219 name[len] = NUL;
220 }
221 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
222 if (len > 0)
223 name[len] = cc;
224 if (HASHITEM_EMPTY(hi))
225 return NULL;
226
227 sav = HI2SAV(hi);
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +0200228 if (sav->sav_block_id == 0)
229 // variable defined in the top script scope is always visible
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200230 return sav;
231
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +0200232 if (cctx == NULL)
233 {
Bram Moolenaar766ae5b2022-09-14 00:30:51 +0100234 if (cstack == NULL)
235 return NULL;
236
Bram Moolenaardce24412022-02-08 20:35:30 +0000237 // Not in a function scope, find variable with block ID equal to or
Bram Moolenaar7a3b8022022-02-14 19:53:03 +0000238 // smaller than the current block id. Use "cstack" to go up the block
239 // scopes.
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +0200240 while (sav != NULL)
241 {
Bram Moolenaar7a3b8022022-02-14 19:53:03 +0000242 int idx;
Bram Moolenaardce24412022-02-08 20:35:30 +0000243
Bram Moolenaar7a3b8022022-02-14 19:53:03 +0000244 for (idx = cstack->cs_idx; idx >= 0; --idx)
245 if (cstack->cs_block_id[idx] == sav->sav_block_id)
Bram Moolenaardce24412022-02-08 20:35:30 +0000246 break;
Bram Moolenaar7a3b8022022-02-14 19:53:03 +0000247 if (idx >= 0)
Bram Moolenaar3c77b6a2021-07-25 18:07:00 +0200248 break;
249 sav = sav->sav_next;
250 }
251 return sav;
252 }
253
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200254 // Go over the variables with this name and find one that was visible
255 // from the function.
256 ufunc = cctx->ctx_ufunc;
257 while (sav != NULL)
258 {
259 int idx;
260
261 // Go over the blocks that this function was defined in. If the
262 // variable block ID matches it was visible to the function.
263 for (idx = 0; idx < ufunc->uf_block_depth; ++idx)
264 if (ufunc->uf_block_ids[idx] == sav->sav_block_id)
265 return sav;
266 sav = sav->sav_next;
267 }
268
Bram Moolenaar58493cf2022-01-06 12:23:30 +0000269 // Not found, variable was not visible.
270 return NULL;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200271}
272
273/*
Bram Moolenaar766ae5b2022-09-14 00:30:51 +0100274 * If "name" can be found in the current script set it's "block_id".
275 */
276 void
277update_script_var_block_id(char_u *name, int block_id)
278{
279 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
280 hashitem_T *hi;
281 sallvar_T *sav;
282
283 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
284 if (HASHITEM_EMPTY(hi))
285 return;
286 sav = HI2SAV(hi);
287 sav->sav_block_id = block_id;
288}
289
290/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200291 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaar7a3b8022022-02-14 19:53:03 +0000292 * "cctx" is NULL at the script level, "cstack" is NULL in a function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100293 * Returns OK or FAIL.
294 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000295 int
Bram Moolenaardce24412022-02-08 20:35:30 +0000296script_var_exists(char_u *name, size_t len, cctx_T *cctx, cstack_T *cstack)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100297{
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200298 if (current_sctx.sc_sid <= 0)
299 return FAIL;
Hirohito Higashif5bfc482025-05-01 08:56:39 +0200300 if (current_script_is_vim9())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200301 {
302 // Check script variables that were visible where the function was
303 // defined.
Bram Moolenaardce24412022-02-08 20:35:30 +0000304 if (find_script_var(name, len, cctx, cstack) != NULL)
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200305 return OK;
306 }
307 else
308 {
309 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
310 dictitem_T *di;
311 int cc;
312
313 // Check script variables that are currently visible
314 cc = name[len];
315 name[len] = NUL;
316 di = find_var_in_ht(ht, 0, name, TRUE);
317 name[len] = cc;
318 if (di != NULL)
319 return OK;
320 }
321
322 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100323}
324
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100325/*
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200326 * Returns the index of a class method or class variable with name "name"
327 * accessible in the currently compiled function.
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200328 * If "cl_ret" is not NULL set it to the class.
329 * Otherwise return -1.
330 */
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200331 static int
332cctx_class_midx(
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200333 cctx_T *cctx,
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200334 int is_method,
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200335 char_u *name,
336 size_t len,
337 class_T **cl_ret)
338{
339 if (cctx == NULL || cctx->ctx_ufunc == NULL
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200340 || cctx->ctx_ufunc->uf_class == NULL
341 || cctx->ctx_ufunc->uf_defclass == NULL)
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200342 return -1;
343
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200344 // Search for the class method or variable in the class where the calling
345 // function is defined.
346 class_T *cl = cctx->ctx_ufunc->uf_defclass;
347 int m_idx = is_method ? class_method_idx(cl, name, len)
348 : class_member_idx(cl, name, len);
349 if (m_idx < 0)
350 {
351 cl = cl->class_extends;
352 while (cl != NULL)
353 {
354 m_idx = is_method ? class_method_idx(cl, name, len)
355 : class_member_idx(cl, name, len);
356 if (m_idx >= 0)
357 break;
358 cl = cl->class_extends;
359 }
360 }
361
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200362 if (m_idx >= 0)
363 {
364 if (cl_ret != NULL)
365 *cl_ret = cl;
366 }
367
368 return m_idx;
369}
370
371/*
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200372 * Returns the index of a class method with name "name" accessible in the
373 * currently compiled function. Returns -1 if not found. The class where the
374 * method is defined is returned in "cl_ret".
375 */
376 int
377cctx_class_method_idx(
378 cctx_T *cctx,
379 char_u *name,
380 size_t len,
381 class_T **cl_ret)
382{
383 return cctx_class_midx(cctx, TRUE, name, len, cl_ret);
384}
385
386/*
387 * Returns the index of a class variable with name "name" accessible in the
388 * currently compiled function. Returns -1 if not found. The class where the
389 * variable is defined is returned in "cl_ret".
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200390 */
391 int
392cctx_class_member_idx(
393 cctx_T *cctx,
394 char_u *name,
395 size_t len,
396 class_T **cl_ret)
397{
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200398 return cctx_class_midx(cctx, FALSE, name, len, cl_ret);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200399}
400
401/*
Bram Moolenaare0890d62021-02-17 14:52:14 +0100402 * Return TRUE if "name" is a local variable, argument, script variable or
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +0000403 * imported. Also if "name" is "this" and in a class method.
Bram Moolenaare0890d62021-02-17 14:52:14 +0100404 */
405 static int
406variable_exists(char_u *name, size_t len, cctx_T *cctx)
407{
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100408 return (cctx != NULL
409 && (lookup_local(name, len, NULL, cctx) == OK
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000410 || arg_exists(name, len, NULL, NULL, NULL, cctx) == OK
411 || (len == 4
412 && cctx->ctx_ufunc != NULL
Bram Moolenaar574950d2023-01-03 19:08:50 +0000413 && (cctx->ctx_ufunc->uf_flags & (FC_OBJECT|FC_NEW))
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000414 && STRNCMP(name, "this", 4) == 0)))
Bram Moolenaardce24412022-02-08 20:35:30 +0000415 || script_var_exists(name, len, cctx, NULL) == OK
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200416 || cctx_class_member_idx(cctx, name, len, NULL) >= 0
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000417 || find_imported(name, len, FALSE) != NULL;
Bram Moolenaare0890d62021-02-17 14:52:14 +0100418}
419
420/*
Bram Moolenaar6914e872021-03-06 21:01:09 +0100421 * Return TRUE if "name" is a local variable, argument, script variable,
Bram Moolenaar97f8c102022-04-02 19:43:57 +0100422 * imported or function. Or commands are being skipped, a declaration may have
423 * been skipped then.
Bram Moolenaar6914e872021-03-06 21:01:09 +0100424 */
425 static int
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100426item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100427{
Bram Moolenaar0631bb42022-02-13 21:20:21 +0000428 return variable_exists(name, len, cctx);
Bram Moolenaar6914e872021-03-06 21:01:09 +0100429}
430
431/*
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100432 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaar7a3b8022022-02-14 19:53:03 +0000433 * compilation context "cctx".
434 * "cctx" is NULL at the script level, "cstack" is NULL in a function.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200435 * Does not check the global namespace.
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100436 * If "is_arg" is TRUE the error message is for an argument name.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100437 * Return FAIL and give an error if it defined.
438 */
439 int
Bram Moolenaardce24412022-02-08 20:35:30 +0000440check_defined(
441 char_u *p,
442 size_t len,
443 cctx_T *cctx,
444 cstack_T *cstack,
445 int is_arg)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100446{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200447 int c = p[len];
448 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200449
Bram Moolenaarda479c72021-04-10 21:01:38 +0200450 // underscore argument is OK
451 if (len == 1 && *p == '_')
452 return OK;
453
Bram Moolenaardce24412022-02-08 20:35:30 +0000454 if (script_var_exists(p, len, cctx, cstack) == OK)
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100455 {
456 if (is_arg)
457 semsg(_(e_argument_already_declared_in_script_str), p);
458 else
459 semsg(_(e_variable_already_declared_in_script_str), p);
460 return FAIL;
461 }
462
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200463 if (cctx_class_member_idx(cctx, p, len, NULL) >= 0)
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000464 {
465 if (is_arg)
466 semsg(_(e_argument_already_declared_in_class_str), p);
467 else
468 semsg(_(e_variable_already_declared_in_class_str), p);
469 return FAIL;
470 }
471
Bram Moolenaarad486a02020-08-01 23:22:18 +0200472 p[len] = NUL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100473 if ((cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100474 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200475 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000476 || find_imported(p, len, FALSE) != NULL
Bram Moolenaaracc4b562022-01-24 13:54:45 +0000477 || (ufunc = find_func_even_dead(p, 0)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100478 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200479 // A local or script-local function can shadow a global function.
Bram Moolenaar577dc932021-06-27 15:35:40 +0200480 if (ufunc == NULL || ((ufunc->uf_flags & FC_DEAD) == 0
481 && (!func_is_global(ufunc)
482 || (p[0] == 'g' && p[1] == ':'))))
Bram Moolenaar0f769812020-09-12 18:32:34 +0200483 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100484 if (is_arg)
485 semsg(_(e_argument_name_shadows_existing_variable_str), p);
486 else
487 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar0f769812020-09-12 18:32:34 +0200488 p[len] = c;
Bram Moolenaar0f769812020-09-12 18:32:34 +0200489 return FAIL;
490 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100491 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200492 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100493 return OK;
494}
495
Bram Moolenaar65b95452020-07-19 14:03:09 +0200496
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100497/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200498 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
499 * used. Return FALSE if the types will never match.
500 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200501 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200502use_typecheck(type_T *actual, type_T *expected)
503{
504 if (actual->tt_type == VAR_ANY
505 || actual->tt_type == VAR_UNKNOWN
506 || (actual->tt_type == VAR_FUNC
507 && (expected->tt_type == VAR_FUNC
508 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000509 && (actual->tt_member == &t_any
510 || actual->tt_member == &t_unknown
511 || actual->tt_argcount < 0)
512 && (actual->tt_member == &t_unknown ||
513 (actual->tt_member == &t_void)
Bram Moolenaar328eac22021-01-07 19:23:08 +0100514 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200515 return TRUE;
LemonBoy50d48542024-07-04 17:03:17 +0200516 if (actual->tt_type == VAR_OBJECT && expected->tt_type == VAR_OBJECT)
517 return TRUE;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100518 if ((actual->tt_type == VAR_LIST
519 || actual->tt_type == VAR_TUPLE
520 || actual->tt_type == VAR_DICT)
521 && actual->tt_type == expected->tt_type)
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200522 // This takes care of a nested list or dict.
523 return use_typecheck(actual->tt_member, expected->tt_member);
524 return FALSE;
525}
526
527/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200528 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200529 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200530 * - "actual" is a type that can be "expected" type: add a runtime check; or
531 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200532 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
533 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200534 */
Bram Moolenaara1c51952022-02-02 16:20:26 +0000535 int
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200536need_type_where(
537 type_T *actual,
538 type_T *expected,
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000539 int number_ok, // expect VAR_FLOAT but VAR_NUMBER is OK
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200540 int offset,
541 where_T where,
542 cctx_T *cctx,
543 int silent,
544 int actual_is_const)
545{
Bram Moolenaar44a89772021-12-18 12:31:33 +0000546 int ret;
547
Ernie Raele75fde62023-12-21 17:18:54 +0100548 if (expected->tt_type != VAR_CLASS && expected->tt_type != VAR_TYPEALIAS)
549 {
550 if (check_type_is_value(actual) == FAIL)
551 return FAIL;
552 }
553
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200554 if (expected == &t_bool && actual != &t_bool
555 && (actual->tt_flags & TTFLAG_BOOL_OK))
556 {
557 // Using "0", "1" or the result of an expression with "&&" or "||" as a
558 // boolean is OK but requires a conversion.
559 generate_2BOOL(cctx, FALSE, offset);
560 return OK;
561 }
562
Bram Moolenaar44a89772021-12-18 12:31:33 +0000563 ret = check_type_maybe(expected, actual, FALSE, where);
564 if (ret == OK)
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200565 return OK;
566
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000567 // If actual a constant a runtime check makes no sense. If it's
568 // null_function it is OK.
569 if (actual_is_const && ret == MAYBE && actual == &t_func_unknown)
570 return OK;
571
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200572 // If the actual type can be the expected type add a runtime check.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000573 if (!actual_is_const && ret == MAYBE && use_typecheck(actual, expected))
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200574 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000575 generate_TYPECHECK(cctx, expected, number_ok, offset,
LemonBoyc5d27442023-08-19 13:02:35 +0200576 where.wt_kind == WT_VARIABLE, where.wt_index);
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200577 return OK;
578 }
579
580 if (!silent)
581 type_mismatch_where(expected, actual, where);
582 return FAIL;
583}
584
Bram Moolenaar351ead02021-01-16 16:07:01 +0100585 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200586need_type(
587 type_T *actual,
588 type_T *expected,
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000589 int number_ok, // when expected is float number is also OK
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200590 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +0100591 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200592 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200593 int silent,
594 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200595{
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200596 where_T where = WHERE_INIT;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100597
LemonBoyc5d27442023-08-19 13:02:35 +0200598 if (arg_idx > 0)
599 {
600 where.wt_index = arg_idx;
601 where.wt_kind = WT_ARGUMENT;
602 }
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000603 return need_type_where(actual, expected, number_ok, offset, where,
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200604 cctx, silent, actual_is_const);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200605}
606
607/*
Bram Moolenaarfa103972022-09-29 19:14:42 +0100608 * Set type of variable "lvar" to "type". If the variable is a constant then
609 * the type gets TTFLAG_CONST.
610 */
611 static void
612set_var_type(lvar_T *lvar, type_T *type_arg, cctx_T *cctx)
613{
614 type_T *type = type_arg;
615
Bram Moolenaar6586a012022-09-30 11:04:50 +0100616 if (lvar->lv_const == ASSIGN_CONST && (type->tt_flags & TTFLAG_CONST) == 0)
Bram Moolenaarfa103972022-09-29 19:14:42 +0100617 {
618 if (type->tt_flags & TTFLAG_STATIC)
619 // entry in static_types[] is followed by const type
620 type = type + 1;
621 else
622 {
623 type = copy_type(type, cctx->ctx_type_list);
624 type->tt_flags |= TTFLAG_CONST;
625 }
626 }
627 lvar->lv_type = type;
628}
629
630/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100631 * Reserve space for a local variable.
Bram Moolenaar6586a012022-09-30 11:04:50 +0100632 * "assign" can be ASSIGN_VAR for :var, ASSIGN_CONST for :const and
633 * ASSIGN_FINAL for :final.
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200634 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100635 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000636 lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +0200637reserve_local(
638 cctx_T *cctx,
639 char_u *name,
640 size_t len,
Bram Moolenaar6586a012022-09-30 11:04:50 +0100641 int assign,
Bram Moolenaare8211a32020-10-09 22:04:29 +0200642 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100643{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100644 lvar_T *lvar;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200645 dfunc_T *dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100646
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200647 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100648 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200649 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200650 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100651 }
652
Bram Moolenaar35578162021-08-02 19:10:38 +0200653 if (GA_GROW_FAILS(&cctx->ctx_locals, 1))
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200654 return NULL;
655 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +0200656 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100657
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200658 // Every local variable uses the next entry on the stack. We could re-use
659 // the last ones when leaving a scope, but then variables used in a closure
660 // might get overwritten. To keep things simple do not re-use stack
661 // entries. This is less efficient, but memory is cheap these days.
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200662 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx;
663 lvar->lv_idx = dfunc->df_var_names.ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200664
Bram Moolenaar71ccd032020-06-12 22:59:11 +0200665 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar6586a012022-09-30 11:04:50 +0100666 lvar->lv_const = assign;
Bram Moolenaarfa103972022-09-29 19:14:42 +0100667 if (type == &t_unknown || type == &t_any)
668 // type not known yet, may be inferred from RHS
669 lvar->lv_type = type;
670 else
671 // may use TTFLAG_CONST
672 set_var_type(lvar, type, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100673
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200674 // Remember the name for debugging.
Bram Moolenaar35578162021-08-02 19:10:38 +0200675 if (GA_GROW_FAILS(&dfunc->df_var_names, 1))
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200676 return NULL;
677 ((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] =
678 vim_strsave(lvar->lv_name);
679 ++dfunc->df_var_names.ga_len;
680
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200681 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100682}
683
684/*
Bram Moolenaar08251752021-01-11 21:20:18 +0100685 * If "check_writable" is ASSIGN_CONST give an error if the variable was
686 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
687 * error if the variable was defined with :const.
688 */
689 static int
690check_item_writable(svar_T *sv, int check_writable, char_u *name)
691{
692 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
693 || (check_writable == ASSIGN_FINAL
694 && sv->sv_const == ASSIGN_CONST))
695 {
Bram Moolenaard8e44472021-07-21 22:20:33 +0200696 semsg(_(e_cannot_change_readonly_variable_str), name);
Bram Moolenaar08251752021-01-11 21:20:18 +0100697 return FAIL;
698 }
699 return OK;
700}
701
702/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100703 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +0100704 * Pass "check_writable" to check_item_writable().
Bram Moolenaar7a3b8022022-02-14 19:53:03 +0000705 * "cctx" is NULL at the script level, "cstack" is NULL in a function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100706 * Returns the index in "sn_var_vals" if found.
707 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +0100708 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100709 */
710 int
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000711get_script_item_idx(
712 int sid,
713 char_u *name,
714 int check_writable,
715 cctx_T *cctx,
716 cstack_T *cstack)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100717{
718 hashtab_T *ht;
719 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100720 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200721 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100722 int idx;
723
Bram Moolenaare3d46852020-08-29 13:39:17 +0200724 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100725 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200726 if (sid == current_sctx.sc_sid)
727 {
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000728 sallvar_T *sav = find_script_var(name, 0, cctx, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200729
730 if (sav == NULL)
731 return -2;
732 idx = sav->sav_var_vals_idx;
733 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +0100734 if (check_item_writable(sv, check_writable, name) == FAIL)
735 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200736 return idx;
737 }
738
739 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100740 ht = &SCRIPT_VARS(sid);
741 di = find_var_in_ht(ht, 0, name, TRUE);
742 if (di == NULL)
Bram Moolenaar577bd852022-01-10 18:42:52 +0000743 {
744 if (si->sn_autoload_prefix != NULL)
745 {
746 hashitem_T *hi;
747
748 // A variable exported from an autoload script is in the global
749 // variables, we can find it in the all_vars table.
750 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
751 if (!HASHITEM_EMPTY(hi))
752 return HI2SAV(hi)->sav_var_vals_idx;
753 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100754 return -2;
Bram Moolenaar577bd852022-01-10 18:42:52 +0000755 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100756
757 // Now find the svar_T index in sn_var_vals.
758 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
759 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200760 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100761 if (sv->sv_tv == &di->di_tv)
762 {
Bram Moolenaar08251752021-01-11 21:20:18 +0100763 if (check_item_writable(sv, check_writable, name) == FAIL)
764 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100765 return idx;
766 }
767 }
768 return -1;
769}
770
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000771 static imported_T *
772find_imported_in_script(char_u *name, size_t len, int sid)
773{
774 scriptitem_T *si;
775 int idx;
776
777 if (!SCRIPT_ID_VALID(sid))
778 return NULL;
779 si = SCRIPT_ITEM(sid);
780 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
781 {
782 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
783
784 if (len == 0 ? STRCMP(name, import->imp_name) == 0
785 : STRLEN(import->imp_name) == len
786 && STRNCMP(name, import->imp_name, len) == 0)
787 return import;
788 }
789 return NULL;
790}
791
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100792/*
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000793 * Find "name" in imported items of the current script.
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100794 * If "len" is 0 use any length that works.
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000795 * If "load" is TRUE and the script was not loaded yet, load it now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100796 */
797 imported_T *
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000798find_imported(char_u *name, size_t len, int load)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100799{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200800 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +0200801 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100802
Bram Moolenaarb775e722022-11-22 18:12:44 +0000803 // Skip over "s:" before "s:something" to find the import name.
804 int off = name[0] == 's' && name[1] == ':' ? 2 : 0;
805
806 imported_T *ret = find_imported_in_script(name + off, len - off,
807 current_sctx.sc_sid);
Bram Moolenaarb697dc22022-01-22 11:27:29 +0000808 if (ret != NULL && load && (ret->imp_flags & IMP_FLAGS_AUTOLOAD))
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000809 {
Bram Moolenaar753885b2022-08-24 16:30:36 +0100810 scid_T actual_sid = 0;
Bram Moolenaar6809ff92022-07-26 15:10:56 +0100811 int save_emsg_off = emsg_off;
812
813 // "emsg_off" will be set when evaluating an expression silently, but
814 // we do want to know about errors in a script. Also because it then
815 // aborts when an error is encountered.
816 emsg_off = FALSE;
Bram Moolenaar17d36cb2022-01-12 11:46:40 +0000817
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000818 // script found before but not loaded yet
Bram Moolenaarb697dc22022-01-22 11:27:29 +0000819 ret->imp_flags &= ~IMP_FLAGS_AUTOLOAD;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000820 (void)do_source(SCRIPT_ITEM(ret->imp_sid)->sn_name, FALSE,
Bram Moolenaar753885b2022-08-24 16:30:36 +0100821 DOSO_NONE, &actual_sid);
822 // If the script is a symlink it may be sourced with another name, may
823 // need to adjust the script ID for that.
824 if (actual_sid != 0)
825 ret->imp_sid = actual_sid;
Bram Moolenaar6809ff92022-07-26 15:10:56 +0100826
827 emsg_off = save_emsg_off;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000828 }
829 return ret;
Bram Moolenaarefa94442020-08-08 22:16:00 +0200830}
831
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100832/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000833 * Called when checking for a following operator at "arg". When the rest of
834 * the line is empty or only a comment, peek the next line. If there is a next
835 * line return a pointer to it and set "nextp".
836 * Otherwise skip over white space.
837 */
838 char_u *
839may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
840{
841 char_u *p = skipwhite(arg);
842
843 *nextp = NULL;
844 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
845 {
846 *nextp = peek_next_line_from_context(cctx);
847 if (*nextp != NULL)
848 return *nextp;
849 }
850 return p;
851}
852
853/*
Bram Moolenaar23c55272020-06-21 16:58:13 +0200854 * Return a pointer to the next line that isn't empty or only contains a
855 * comment. Skips over white space.
856 * Returns NULL if there is none.
857 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200858 char_u *
859peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +0200860{
861 int lnum = cctx->ctx_lnum;
862
863 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
864 {
865 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +0200866 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +0200867
Bram Moolenaarba60cc42020-08-12 19:15:33 +0200868 // ignore NULLs inserted for continuation lines
869 if (line != NULL)
870 {
871 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100872 if (vim9_bad_comment(p))
873 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +0200874 if (*p != NUL && !vim9_comment_start(p))
875 return p;
876 }
Bram Moolenaar23c55272020-06-21 16:58:13 +0200877 }
878 return NULL;
879}
880
881/*
Bram Moolenaare6085c52020-04-12 20:19:16 +0200882 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +0200883 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +0200884 * Returns NULL when at the end.
885 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200886 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +0200887next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +0200888{
Bram Moolenaar7a092242020-04-16 22:10:49 +0200889 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +0200890
891 do
892 {
893 ++cctx->ctx_lnum;
894 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +0200895 {
896 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +0200897 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +0200898 }
Bram Moolenaare6085c52020-04-12 20:19:16 +0200899 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +0200900 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200901 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +0200902 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +0200903 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +0200904 return line;
905}
906
907/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +0100908 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200909 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +0200910 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200911 * Return FAIL if beyond the last line, "*arg" is unmodified then.
912 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000913 int
Bram Moolenaar2c330432020-04-13 14:41:35 +0200914may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200915{
Bram Moolenaar5afd0812021-01-03 18:33:13 +0100916 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100917 if (vim9_bad_comment(*arg))
918 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200919 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200920 {
Bram Moolenaar23c55272020-06-21 16:58:13 +0200921 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200922
923 if (next == NULL)
924 return FAIL;
925 *arg = skipwhite(next);
926 }
927 return OK;
928}
929
Bram Moolenaara7eedf32020-07-10 21:50:41 +0200930/*
931 * Idem, and give an error when failed.
932 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000933 int
Bram Moolenaara7eedf32020-07-10 21:50:41 +0200934may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
935{
936 if (may_get_next_line(whitep, arg, cctx) == FAIL)
937 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +0100938 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200939 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +0200940 return FAIL;
941 }
942 return OK;
943}
944
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100945/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200946 * Get a line from the compilation context, compatible with exarg_T getline().
947 * Return a pointer to the line in allocated memory.
948 * Return NULL for end-of-file or some error.
949 */
950 static char_u *
951exarg_getline(
952 int c UNUSED,
953 void *cookie,
954 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200955 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +0200956{
957 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +0200958 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +0200959
Bram Moolenaar66250c92020-08-20 15:02:42 +0200960 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +0200961 {
Bram Moolenaar2914a202020-09-27 18:24:03 +0200962 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +0200963 return NULL;
964 ++cctx->ctx_lnum;
965 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
966 // Comment lines result in NULL pointers, skip them.
967 if (p != NULL)
968 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +0200969 }
Bram Moolenaar04b12692020-05-04 23:24:44 +0200970}
971
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100972 void
973fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
974{
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100975 eap->ea_getline = exarg_getline;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100976 eap->cookie = cctx;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +0000977 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100978}
979
Bram Moolenaar04b12692020-05-04 23:24:44 +0200980/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000981 * Return TRUE if "ufunc" should be compiled, taking into account whether
982 * "profile" indicates profiling is to be done.
983 */
984 int
985func_needs_compiling(ufunc_T *ufunc, compiletype_T compile_type)
986{
987 switch (ufunc->uf_def_status)
988 {
989 case UF_TO_BE_COMPILED:
990 return TRUE;
991
992 case UF_COMPILED:
993 {
994 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
995 + ufunc->uf_dfunc_idx;
996
997 switch (compile_type)
998 {
999 case CT_PROFILE:
1000#ifdef FEAT_PROFILE
1001 return dfunc->df_instr_prof == NULL;
1002#endif
1003 case CT_NONE:
1004 return dfunc->df_instr == NULL;
1005 case CT_DEBUG:
1006 return dfunc->df_instr_debug == NULL;
1007 }
1008 }
1009
1010 case UF_NOT_COMPILED:
1011 case UF_COMPILE_ERROR:
1012 case UF_COMPILING:
1013 break;
1014 }
1015 return FALSE;
1016}
1017
1018/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02001019 * Compile a nested :def command.
1020 */
1021 static char_u *
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001022compile_nested_function(exarg_T *eap, cctx_T *cctx, garray_T *lines_to_free)
Bram Moolenaar04b12692020-05-04 23:24:44 +02001023{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001024 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02001025 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02001026 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001027 int off;
1028 char_u *func_name;
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +01001029 string_T lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001030 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001031 int r = FAIL;
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02001032 compiletype_T compile_type;
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001033 int funcref_isn_idx = -1;
Bram Moolenaar1889f492022-08-16 19:34:44 +01001034 lvar_T *lvar = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001035
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001036 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02001037 {
Hirohito Higashi83313102025-07-08 23:07:21 +02001038 semsg(_(e_cannot_use_bang_with_nested_def_str),
1039 eap->cmdidx == CMD_def ? ":def" : ":function");
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02001040 return NULL;
1041 }
1042
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001043 if (*name_start == '/')
1044 {
1045 name_end = skip_regexp(name_start + 1, '/', TRUE);
1046 if (*name_end == '/')
1047 ++name_end;
Bram Moolenaar63b91732021-08-05 20:40:03 +02001048 set_nextcmd(eap, name_end);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001049 }
1050 if (name_end == name_start || *skipwhite(name_end) != '(')
1051 {
1052 if (!ends_excmd2(name_start, name_end))
1053 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00001054 if (*skipwhite(name_end) == '.')
1055 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
1056 eap->cmd);
1057 else
1058 semsg(_(e_invalid_command_str), eap->cmd);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001059 return NULL;
1060 }
1061
1062 // "def" or "def Name": list functions
1063 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
1064 return NULL;
1065 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
1066 }
1067
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02001068 // Only g:Func() can use a namespace.
1069 if (name_start[1] == ':' && !is_global)
1070 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001071 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02001072 return NULL;
1073 }
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00001074 if (cctx->ctx_skip != SKIP_YES
1075 && check_defined(name_start, name_end - name_start, cctx,
Bram Moolenaardce24412022-02-08 20:35:30 +00001076 NULL, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02001077 return NULL;
Bram Moolenaarf681cfb2022-02-07 20:30:57 +00001078 if (!ASCII_ISUPPER(is_global ? name_start[2] : name_start[0]))
1079 {
Bram Moolenaar3787f262022-02-07 21:54:01 +00001080 semsg(_(e_function_name_must_start_with_capital_str), name_start);
Bram Moolenaarf681cfb2022-02-07 20:30:57 +00001081 return NULL;
1082 }
Bram Moolenaareef21022020-08-01 22:16:43 +02001083
Bram Moolenaar04b12692020-05-04 23:24:44 +02001084 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001085 fill_exarg_from_cctx(eap, cctx);
1086
Bram Moolenaar04b12692020-05-04 23:24:44 +02001087 eap->forceit = FALSE;
Bram Moolenaar38453522021-11-28 22:00:12 +00001088 // We use the special <Lamba>99 name, but it's not really a lambda.
John Marriottb32800f2025-02-01 15:25:34 +01001089 lambda_name = get_lambda_name();
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +01001090 lambda_name.string = vim_strnsave(lambda_name.string, lambda_name.length);
1091 if (lambda_name.string == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001092 return NULL;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001093
1094 // This may free the current line, make a copy of the name.
1095 off = is_global ? 2 : 0;
1096 func_name = vim_strnsave(name_start + off, name_end - name_start - off);
1097 if (func_name == NULL)
1098 {
1099 r = FAIL;
1100 goto theend;
1101 }
1102
Bram Moolenaar39c82ea2023-01-02 13:08:01 +00001103 // Make sure "KeyTyped" is not set, it may cause indent to be written.
1104 int save_KeyTyped = KeyTyped;
1105 KeyTyped = FALSE;
1106
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +01001107 ufunc = define_function(eap, lambda_name.string, lines_to_free, 0, NULL, 0);
Bram Moolenaar39c82ea2023-01-02 13:08:01 +00001108
1109 KeyTyped = save_KeyTyped;
1110
Bram Moolenaar822ba242020-05-24 23:00:18 +02001111 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001112 {
1113 r = eap->skip ? OK : FAIL;
1114 goto theend;
1115 }
Bram Moolenaar7473a842021-12-28 17:55:26 +00001116 if (eap->nextcmd != NULL)
1117 {
1118 semsg(_(e_text_found_after_str_str),
1119 eap->cmdidx == CMD_def ? "enddef" : "endfunction", eap->nextcmd);
1120 r = FAIL;
Bram Moolenaard2939812021-12-30 17:09:05 +00001121 func_ptr_unref(ufunc);
Bram Moolenaar7473a842021-12-28 17:55:26 +00001122 goto theend;
1123 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01001124
1125 // copy over the block scope IDs before compiling
1126 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
1127 {
1128 int block_depth = cctx->ctx_ufunc->uf_block_depth;
1129
1130 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
1131 if (ufunc->uf_block_ids != NULL)
1132 {
1133 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
1134 sizeof(int) * block_depth);
1135 ufunc->uf_block_depth = block_depth;
1136 }
1137 }
1138
Bram Moolenaara915fa02022-03-23 11:29:15 +00001139 // Define the funcref before compiling, so that it is found by any
1140 // recursive call.
1141 if (is_global)
1142 {
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +01001143 r = generate_NEWFUNC(cctx, lambda_name.string, func_name);
Bram Moolenaara915fa02022-03-23 11:29:15 +00001144 func_name = NULL;
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +01001145 lambda_name.string = NULL;
1146 lambda_name.length = 0;
Bram Moolenaara915fa02022-03-23 11:29:15 +00001147 }
1148 else
1149 {
1150 // Define a local variable for the function reference.
Bram Moolenaar1889f492022-08-16 19:34:44 +01001151 lvar = reserve_local(cctx, func_name, name_end - name_start,
Bram Moolenaar6586a012022-09-30 11:04:50 +01001152 ASSIGN_CONST, ufunc->uf_func_type);
Bram Moolenaara915fa02022-03-23 11:29:15 +00001153 if (lvar == NULL)
1154 goto theend;
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001155 if (generate_FUNCREF(cctx, ufunc, NULL, FALSE, 0, &funcref_isn_idx) == FAIL)
Bram Moolenaara915fa02022-03-23 11:29:15 +00001156 goto theend;
1157 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
1158 }
1159
Bram Moolenaar139575d2022-03-15 19:29:30 +00001160 compile_type = get_compile_type(ufunc);
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02001161#ifdef FEAT_PROFILE
1162 // If the outer function is profiled, also compile the nested function for
1163 // profiling.
1164 if (cctx->ctx_compile_type == CT_PROFILE)
1165 compile_type = CT_PROFILE;
1166#endif
1167 if (func_needs_compiling(ufunc, compile_type)
1168 && compile_def_function(ufunc, TRUE, compile_type, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02001169 {
1170 func_ptr_unref(ufunc);
Bram Moolenaar1889f492022-08-16 19:34:44 +01001171 if (lvar != NULL)
1172 // Now the local variable can't be used.
1173 *lvar->lv_name = '/'; // impossible value
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001174 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02001175 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02001176
Bram Moolenaar648594e2021-07-11 17:55:01 +02001177#ifdef FEAT_PROFILE
1178 // When the outer function is compiled for profiling, the nested function
1179 // may be called without profiling. Compile it here in the right context.
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02001180 if (compile_type == CT_PROFILE && func_needs_compiling(ufunc, CT_NONE))
Bram Moolenaar648594e2021-07-11 17:55:01 +02001181 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1182#endif
1183
Bram Moolenaara915fa02022-03-23 11:29:15 +00001184 // If a FUNCREF instruction was generated, set the index after compiling.
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001185 if (funcref_isn_idx != -1 && ufunc->uf_def_status == UF_COMPILED)
1186 {
1187 isn_T *funcref_isn = ((isn_T *)cctx->ctx_instr.ga_data) +
1188 funcref_isn_idx;
Bram Moolenaara915fa02022-03-23 11:29:15 +00001189 funcref_isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001190 }
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001191
1192theend:
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +01001193 vim_free(lambda_name.string);
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001194 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001195 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02001196}
1197
1198/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001199 * Compile one Vim expression {expr} in string "p".
1200 * "p" points to the opening "{".
1201 * Return a pointer to the character after "}", NULL for an error.
1202 */
1203 char_u *
1204compile_one_expr_in_str(char_u *p, cctx_T *cctx)
1205{
1206 char_u *block_start;
1207 char_u *block_end;
1208
1209 // Skip the opening {.
1210 block_start = skipwhite(p + 1);
1211 block_end = block_start;
1212 if (*block_start != NUL && skip_expr(&block_end, NULL) == FAIL)
1213 return NULL;
1214 block_end = skipwhite(block_end);
1215 // The block must be closed by a }.
1216 if (*block_end != '}')
1217 {
1218 semsg(_(e_missing_close_curly_str), p);
1219 return NULL;
1220 }
1221 if (compile_expr0(&block_start, cctx) == FAIL)
1222 return NULL;
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02001223 may_generate_2STRING(-1, TOSTRING_INTERPOLATE, cctx);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001224
1225 return block_end + 1;
1226}
1227
1228/*
LemonBoy2eaef102022-05-06 13:14:50 +01001229 * Compile a string "str" (either containing a literal string or a mix of
1230 * literal strings and Vim expressions of the form `{expr}`). This is used
1231 * when compiling a heredoc assignment to a variable or an interpolated string
1232 * in a Vim9 def function. Vim9 instructions are generated to push strings,
1233 * evaluate expressions, concatenate them and create a list of lines. When
1234 * "evalstr" is TRUE, Vim expressions in "str" are evaluated.
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001235 */
1236 int
LemonBoy2eaef102022-05-06 13:14:50 +01001237compile_all_expr_in_str(char_u *str, int evalstr, cctx_T *cctx)
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001238{
LemonBoy2eaef102022-05-06 13:14:50 +01001239 char_u *p = str;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001240 char_u *val;
LemonBoy2eaef102022-05-06 13:14:50 +01001241 int count = 0;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001242
1243 if (cctx->ctx_skip == SKIP_YES)
1244 return OK;
1245
LemonBoy2eaef102022-05-06 13:14:50 +01001246 if (!evalstr || *str == NUL)
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001247 {
LemonBoy2eaef102022-05-06 13:14:50 +01001248 // Literal string, possibly empty.
1249 val = *str != NUL ? vim_strsave(str) : NULL;
1250 return generate_PUSHS(cctx, &val);
1251 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001252
LemonBoy2eaef102022-05-06 13:14:50 +01001253 // Push all the string pieces to the stack, followed by a ISN_CONCAT.
1254 while (*p != NUL)
1255 {
1256 char_u *lit_start;
LemonBoy2eaef102022-05-06 13:14:50 +01001257 int escaped_brace = FALSE;
1258
1259 // Look for a block start.
1260 lit_start = p;
1261 while (*p != '{' && *p != '}' && *p != NUL)
1262 ++p;
1263
1264 if (*p != NUL && *p == p[1])
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001265 {
LemonBoy2eaef102022-05-06 13:14:50 +01001266 // Escaped brace, unescape and continue.
1267 // Include the brace in the literal string.
1268 ++p;
1269 escaped_brace = TRUE;
1270 }
1271 else if (*p == '}')
1272 {
1273 semsg(_(e_stray_closing_curly_str), str);
1274 return FAIL;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001275 }
LemonBoy372bcce2022-04-25 12:43:20 +01001276
LemonBoy2eaef102022-05-06 13:14:50 +01001277 // Append the literal part.
1278 if (p != lit_start)
1279 {
1280 val = vim_strnsave(lit_start, (size_t)(p - lit_start));
1281 if (generate_PUSHS(cctx, &val) == FAIL)
1282 return FAIL;
1283 ++count;
1284 }
1285
1286 if (*p == NUL)
1287 break;
1288
1289 if (escaped_brace)
1290 {
1291 // Skip the second brace.
1292 ++p;
1293 continue;
1294 }
1295
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001296 p = compile_one_expr_in_str(p, cctx);
1297 if (p == NULL)
LemonBoy2eaef102022-05-06 13:14:50 +01001298 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01001299 ++count;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001300 }
LemonBoy2eaef102022-05-06 13:14:50 +01001301
1302 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001303 if (count > 1)
LemonBoy2eaef102022-05-06 13:14:50 +01001304 return generate_CONCAT(cctx, count);
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001305
1306 return OK;
1307}
1308
1309/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001310 * Return the length of an assignment operator, or zero if there isn't one.
1311 */
1312 int
1313assignment_len(char_u *p, int *heredoc)
1314{
1315 if (*p == '=')
1316 {
1317 if (p[1] == '<' && p[2] == '<')
1318 {
1319 *heredoc = TRUE;
1320 return 3;
1321 }
1322 return 1;
1323 }
1324 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
1325 return 2;
1326 if (STRNCMP(p, "..=", 3) == 0)
1327 return 3;
1328 return 0;
1329}
1330
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001331/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001332 * Generate the load instruction for "name".
1333 */
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001334 static int
Bram Moolenaard505d172022-12-18 21:42:55 +00001335generate_loadvar(cctx_T *cctx, lhs_T *lhs)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001336{
Bram Moolenaard505d172022-12-18 21:42:55 +00001337 char_u *name = lhs->lhs_name;
1338 type_T *type = lhs->lhs_type;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001339 int res = OK;
Bram Moolenaard505d172022-12-18 21:42:55 +00001340
1341 switch (lhs->lhs_dest)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001342 {
1343 case dest_option:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001344 case dest_func_option:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001345 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1346 break;
1347 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01001348 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001349 {
1350 if (name[2] == NUL)
1351 generate_instr_type(cctx, ISN_LOADGDICT, &t_dict_any);
1352 else
1353 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
1354 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01001355 else
1356 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001357 break;
1358 case dest_buffer:
1359 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
1360 break;
1361 case dest_window:
1362 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
1363 break;
1364 case dest_tab:
1365 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
1366 break;
1367 case dest_script:
Ernie Rael3f821d62024-04-24 20:07:50 +02001368 case dest_script_v9:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001369 res = compile_load_scriptvar(cctx,
Hirohito Higashi6c012832025-02-25 20:29:50 +01001370 name + (name[1] == ':' ? 2 : 0), NULL, NULL, NULL);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001371 break;
1372 case dest_env:
1373 // Include $ in the name here
1374 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
1375 break;
1376 case dest_reg:
1377 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
1378 break;
1379 case dest_vimvar:
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001380 generate_LOADV(cctx, name + 2);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001381 break;
1382 case dest_local:
Bram Moolenaar4875d6a2022-08-17 15:55:51 +01001383 if (cctx->ctx_skip != SKIP_YES)
1384 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001385 lvar_T *lvar = lhs->lhs_lvar;
Bram Moolenaar4875d6a2022-08-17 15:55:51 +01001386 if (lvar->lv_from_outer > 0)
1387 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
Bram Moolenaar65449bd2022-09-19 16:02:43 +01001388 lvar->lv_loop_depth, lvar->lv_loop_idx, type);
Bram Moolenaar4875d6a2022-08-17 15:55:51 +01001389 else
1390 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
1391 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001392 break;
Bram Moolenaard505d172022-12-18 21:42:55 +00001393 case dest_class_member:
1394 generate_CLASSMEMBER(cctx, TRUE, lhs->lhs_class,
1395 lhs->lhs_classmember_idx);
1396 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01001397 case dest_expr:
1398 // list or dict value should already be on the stack.
1399 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001400 }
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001401
1402 return res;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001403}
1404
Bram Moolenaardc234ca2020-11-28 18:52:33 +01001405/*
1406 * Skip over "[expr]" or ".member".
1407 * Does not check for any errors.
1408 */
1409 static char_u *
1410skip_index(char_u *start)
1411{
1412 char_u *p = start;
1413
1414 if (*p == '[')
1415 {
1416 p = skipwhite(p + 1);
1417 (void)skip_expr(&p, NULL);
1418 p = skipwhite(p);
1419 if (*p == ']')
1420 return p + 1;
1421 return p;
1422 }
1423 // if (*p == '.')
1424 return to_name_end(p + 1, TRUE);
1425}
1426
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001427 void
1428vim9_declare_error(char_u *name)
1429{
1430 char *scope = "";
1431
1432 switch (*name)
1433 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02001434 case 'g': scope = _("global"); break;
1435 case 'b': scope = _("buffer"); break;
1436 case 'w': scope = _("window"); break;
1437 case 't': scope = _("tab"); break;
1438 case 'v': scope = "v:"; break;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001439 case '$': semsg(_(e_cannot_declare_an_environment_variable_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001440 return;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001441 case '&': semsg(_(e_cannot_declare_an_option_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001442 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001443 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001444 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02001445 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001446 }
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001447 semsg(_(e_cannot_declare_a_scope_variable_str), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001448}
1449
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001450/*
Bram Moolenaar3558afe2022-10-13 16:12:57 +01001451 * Return TRUE if "name" is a valid register to use.
1452 * Return FALSE and give an error message if not.
1453 */
1454 static int
1455valid_dest_reg(int name)
1456{
1457 if ((name == '@' || valid_yank_reg(name, FALSE)) && name != '.')
1458 return TRUE;
1459 emsg_invreg(name);
1460 return FAIL;
1461}
1462
1463/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001464 * For one assignment figure out the type of destination. Return it in "dest".
1465 * When not recognized "dest" is not set.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001466 * For an option "option_scope" is set.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001467 * For a v:var "vimvaridx" is set.
1468 * "type" is set to the destination type if known, unchanted otherwise.
1469 * Return FAIL if an error message was given.
1470 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001471 int
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001472get_var_dest(
1473 char_u *name,
1474 assign_dest_T *dest,
Bram Moolenaar97f8c102022-04-02 19:43:57 +01001475 cmdidx_T cmdidx,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001476 int *option_scope,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001477 int *vimvaridx,
1478 type_T **type,
1479 cctx_T *cctx)
1480{
1481 char_u *p;
1482
1483 if (*name == '&')
1484 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001485 int cc;
1486 long numval;
1487 getoption_T opt_type;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001488 int opt_p_flags;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001489
1490 *dest = dest_option;
1491 if (cmdidx == CMD_final || cmdidx == CMD_const)
1492 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001493 emsg(_(e_cannot_lock_option));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001494 return FAIL;
1495 }
1496 p = name;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001497 p = find_option_end(&p, option_scope);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001498 if (p == NULL)
1499 {
1500 // cannot happen?
Bram Moolenaar108010a2021-06-27 22:03:33 +02001501 emsg(_(e_unexpected_characters_in_assignment));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001502 return FAIL;
1503 }
1504 cc = *p;
1505 *p = NUL;
1506 opt_type = get_option_value(skip_option_env_lead(name),
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001507 &numval, NULL, &opt_p_flags, *option_scope);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001508 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001509 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001510 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001511 case gov_unknown:
Bram Moolenaare1242042021-12-16 20:56:57 +00001512 semsg(_(e_unknown_option_str), name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001513 return FAIL;
1514 case gov_string:
1515 case gov_hidden_string:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001516 if (opt_p_flags & P_FUNC)
1517 {
1518 // might be a Funcref, check the type later
1519 *type = &t_any;
1520 *dest = dest_func_option;
1521 }
1522 else
1523 {
1524 *type = &t_string;
1525 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001526 break;
1527 case gov_bool:
1528 case gov_hidden_bool:
1529 *type = &t_bool;
1530 break;
1531 case gov_number:
1532 case gov_hidden_number:
1533 *type = &t_number;
1534 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001535 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001536 }
1537 else if (*name == '$')
1538 {
1539 *dest = dest_env;
1540 *type = &t_string;
1541 }
1542 else if (*name == '@')
1543 {
Bram Moolenaar3558afe2022-10-13 16:12:57 +01001544 if (!valid_dest_reg(name[1]))
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001545 return FAIL;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001546 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02001547 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001548 }
1549 else if (STRNCMP(name, "g:", 2) == 0)
1550 {
1551 *dest = dest_global;
1552 }
1553 else if (STRNCMP(name, "b:", 2) == 0)
1554 {
1555 *dest = dest_buffer;
1556 }
1557 else if (STRNCMP(name, "w:", 2) == 0)
1558 {
1559 *dest = dest_window;
1560 }
1561 else if (STRNCMP(name, "t:", 2) == 0)
1562 {
1563 *dest = dest_tab;
1564 }
1565 else if (STRNCMP(name, "v:", 2) == 0)
1566 {
1567 typval_T *vtv;
1568 int di_flags;
1569
1570 *vimvaridx = find_vim_var(name + 2, &di_flags);
1571 if (*vimvaridx < 0)
1572 {
1573 semsg(_(e_variable_not_found_str), name);
1574 return FAIL;
1575 }
1576 // We use the current value of "sandbox" here, is that OK?
1577 if (var_check_ro(di_flags, name, FALSE))
1578 return FAIL;
1579 *dest = dest_vimvar;
1580 vtv = get_vim_var_tv(*vimvaridx);
1581 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
1582 }
1583 return OK;
1584}
1585
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001586 static int
Bram Moolenaar97f8c102022-04-02 19:43:57 +01001587is_decl_command(cmdidx_T cmdidx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001588{
1589 return cmdidx == CMD_let || cmdidx == CMD_var
1590 || cmdidx == CMD_final || cmdidx == CMD_const;
1591}
1592
1593/*
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02001594 * Returns TRUE if the class or object variable in "lhs" is modifiable.
1595 * "var_start" points to the start of the variable name and "lhs->lhs_varlen"
1596 * has the total length. Note that the "lhs" can be nested an object reference
1597 * (e.g. a.b.c.d.var).
1598 */
1599 static int
1600lhs_class_member_modifiable(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
1601{
1602 size_t varlen = lhs->lhs_varlen;
1603 class_T *cl = lhs->lhs_type->tt_class;
1604 int is_object = lhs->lhs_type->tt_type == VAR_OBJECT;
1605 char_u *name = var_start + varlen + 1;
1606 size_t namelen = lhs->lhs_end - var_start - varlen - 1;
1607 ocmember_T *m;
1608
1609 m = member_lookup(cl, lhs->lhs_type->tt_type, name, namelen, NULL);
1610 if (m == NULL)
1611 {
1612 member_not_found_msg(cl, lhs->lhs_type->tt_type, name, namelen);
1613 return FALSE;
1614 }
1615
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01001616 if (IS_ENUM(cl))
1617 {
1618 semsg(_(e_enumvalue_str_cannot_be_modified), cl->class_name,
1619 m->ocm_name);
1620 return FALSE;
1621 }
1622
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02001623 // If it is private member variable, then accessing it outside the
1624 // class is not allowed.
1625 // If it is a read only class variable, then it can be modified
1626 // only inside the class where it is defined.
1627 if ((m->ocm_access != VIM_ACCESS_ALL) &&
1628 ((is_object && !inside_class(cctx, cl))
1629 || (!is_object && cctx->ctx_ufunc->uf_class != cl)))
1630 {
1631 char *msg = (m->ocm_access == VIM_ACCESS_PRIVATE)
Ernie Rael03042a22023-11-11 08:53:32 +01001632 ? e_cannot_access_protected_variable_str
RestorerZ7fe8f432023-09-24 23:21:24 +02001633 : e_variable_is_not_writable_str;
Ernie Raele6c9aa52023-10-06 19:55:52 +02001634 emsg_var_cl_define(msg, m->ocm_name, 0, cl);
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02001635 return FALSE;
1636 }
1637
1638 return TRUE;
1639}
1640
1641/*
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001642 * Initialize "lhs" with default values
Bram Moolenaar752fc692021-01-04 21:57:11 +01001643 */
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001644 static void
1645lhs_init_defaults(lhs_T *lhs)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001646{
Bram Moolenaar752fc692021-01-04 21:57:11 +01001647 CLEAR_POINTER(lhs);
1648 lhs->lhs_dest = dest_local;
1649 lhs->lhs_vimvaridx = -1;
1650 lhs->lhs_scriptvar_idx = -1;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001651 lhs->lhs_member_idx = -1;
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001652}
Bram Moolenaar752fc692021-01-04 21:57:11 +01001653
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001654/*
1655 * When compiling a LHS variable name, find the end of the destination and the
1656 * end of the variable name.
1657 */
1658 static int
1659lhs_find_var_end(
1660 lhs_T *lhs,
1661 char_u *var_start,
1662 int is_decl,
1663 char_u **var_endp)
1664{
1665 char_u *var_end = *var_endp;
1666
1667 // "lhs_dest_end" is the end of the destination, including "[expr]" or
Bram Moolenaar752fc692021-01-04 21:57:11 +01001668 // ".name".
1669 // "var_end" is the end of the variable/option/etc. name.
1670 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
1671 if (*var_start == '@')
Bram Moolenaar3558afe2022-10-13 16:12:57 +01001672 {
1673 if (!valid_dest_reg(var_start[1]))
1674 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001675 var_end = var_start + 2;
Bram Moolenaar3558afe2022-10-13 16:12:57 +01001676 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01001677 else
1678 {
1679 // skip over the leading "&", "&l:", "&g:" and "$"
1680 var_end = skip_option_env_lead(var_start);
1681 var_end = to_name_end(var_end, TRUE);
1682 }
1683
1684 // "a: type" is declaring variable "a" with a type, not dict "a:".
1685 if (is_decl && lhs->lhs_dest_end == var_start + 2
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001686 && lhs->lhs_dest_end[-1] == ':')
Bram Moolenaar752fc692021-01-04 21:57:11 +01001687 --lhs->lhs_dest_end;
1688 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
1689 --var_end;
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001690
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00001691 lhs->lhs_end = lhs->lhs_dest_end;
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001692 *var_endp = var_end;
1693
1694 return OK;
1695}
1696
1697/*
1698 * Set various fields in "lhs"
1699 */
1700 static int
1701lhs_init(
1702 lhs_T *lhs,
1703 char_u *var_start,
1704 int is_decl,
1705 int heredoc,
1706 char_u **var_endp)
1707{
1708 char_u *var_end = *var_endp;
1709
1710 lhs_init_defaults(lhs);
1711
1712 // Find the end of the variable and the destination
1713 if (lhs_find_var_end(lhs, var_start, is_decl, &var_end) == FAIL)
1714 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001715
1716 // compute the length of the destination without "[expr]" or ".name"
1717 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02001718 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001719 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
1720 if (lhs->lhs_name == NULL)
1721 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01001722
1723 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
1724 // Something follows after the variable: "var[idx]" or "var.key".
1725 lhs->lhs_has_index = TRUE;
1726
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001727 lhs->lhs_type = heredoc ? &t_list_string : &t_any;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001728
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001729 *var_endp = var_end;
1730
1731 return OK;
1732}
1733
1734/*
1735 * Compile a LHS class variable name.
1736 */
1737 static int
1738compile_lhs_class_variable(
1739 cctx_T *cctx,
1740 lhs_T *lhs,
1741 class_T *defcl,
1742 int is_decl)
1743{
1744 if (cctx->ctx_ufunc->uf_defclass != defcl)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001745 {
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001746 // A class variable can be accessed without the class name
1747 // only inside a class.
1748 semsg(_(e_class_variable_str_accessible_only_inside_class_str),
1749 lhs->lhs_name, defcl->class_name);
1750 return FAIL;
1751 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01001752
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001753 if (is_decl)
1754 {
1755 semsg(_(e_variable_already_declared_in_class_str), lhs->lhs_name);
1756 return FAIL;
1757 }
1758
1759 ocmember_T *m = &defcl->class_class_members[lhs->lhs_classmember_idx];
1760 if (oc_var_check_ro(defcl, m))
1761 return FAIL;
1762
1763 lhs->lhs_dest = dest_class_member;
1764 // The class variable is defined either in the current class or
1765 // in one of the parent class in the hierarchy.
1766 lhs->lhs_class = defcl;
1767 lhs->lhs_type = oc_member_type_by_idx(defcl, FALSE,
1768 lhs->lhs_classmember_idx);
1769
1770 return OK;
1771}
1772
1773/*
1774 * Compile an imported LHS variable
1775 */
1776 static int
1777compile_lhs_import_var(
1778 lhs_T *lhs,
1779 imported_T *import,
1780 char_u *var_start,
1781 char_u **var_endp,
1782 char_u **rawnamep)
1783{
1784 char_u *var_end = *var_endp;
1785 char_u *dot = vim_strchr(var_start, '.');
1786 char_u *p;
1787
1788 // for an import the name is what comes after the dot
1789 if (dot == NULL)
1790 {
1791 semsg(_(e_no_dot_after_imported_name_str), var_start);
1792 return FAIL;
1793 }
1794
1795 p = skipwhite(dot + 1);
1796 var_end = to_name_end(p, TRUE);
1797 if (var_end == p)
1798 {
1799 semsg(_(e_missing_name_after_imported_name_str), var_start);
1800 return FAIL;
1801 }
1802
1803 vim_free(lhs->lhs_name);
1804 lhs->lhs_varlen = var_end - p;
1805 lhs->lhs_name = vim_strnsave(p, lhs->lhs_varlen);
1806 if (lhs->lhs_name == NULL)
1807 return FAIL;
1808 *rawnamep = lhs->lhs_name;
1809 lhs->lhs_scriptvar_sid = import->imp_sid;
1810
1811 // TODO: where do we check this name is exported?
1812
1813 // Check if something follows: "exp.var[idx]" or
1814 // "exp.var.key".
1815 lhs->lhs_has_index = lhs->lhs_dest_end > skipwhite(var_end);
1816
1817 *var_endp = var_end;
1818
1819 return OK;
1820}
1821
1822/*
1823 * Process a script-local variable when compiling a LHS variable name.
1824 */
1825 static int
1826compile_lhs_script_var(
1827 cctx_T *cctx,
1828 lhs_T *lhs,
1829 char_u *var_start,
1830 char_u *var_end,
1831 int is_decl)
1832{
1833 int script_namespace = FALSE;
1834 int script_var = FALSE;
1835 imported_T *import;
1836 char_u *var_name;
Yegappan Lakshmanan084529c2024-12-24 09:50:01 +01001837 size_t var_name_len;
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001838
1839 if (lhs->lhs_varlen > 1 && STRNCMP(var_start, "s:", 2) == 0)
1840 script_namespace = TRUE;
1841
1842 if (script_namespace)
1843 {
1844 var_name = var_start + 2;
1845 var_name_len = lhs->lhs_varlen - 2;
1846 }
1847 else
1848 {
1849 var_name = var_start;
1850 var_name_len = lhs->lhs_varlen;
1851 }
1852
1853 if (script_var_exists(var_name, var_name_len, cctx, NULL) == OK)
1854 script_var = TRUE;
1855
1856 import = find_imported(var_start, lhs->lhs_varlen, FALSE);
1857
1858 if (script_namespace || script_var || import != NULL)
1859 {
1860 char_u *rawname = lhs->lhs_name + (lhs->lhs_name[1] == ':' ? 2 : 0);
1861
1862 if (script_namespace && current_script_is_vim9())
Bram Moolenaar752fc692021-01-04 21:57:11 +01001863 {
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001864 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), var_start);
1865 return FAIL;
1866 }
1867
1868 if (is_decl)
1869 {
1870 if (script_namespace)
1871 semsg(_(e_cannot_declare_script_variable_in_function_str),
1872 lhs->lhs_name);
1873 else
1874 semsg(_(e_variable_already_declared_in_script_str),
1875 lhs->lhs_name);
1876 return FAIL;
1877 }
1878 else if (cctx->ctx_ufunc->uf_script_ctx_version == SCRIPT_VERSION_VIM9
1879 && script_namespace
1880 && !script_var && import == NULL)
1881 {
1882 semsg(_(e_unknown_variable_str), lhs->lhs_name);
1883 return FAIL;
1884 }
1885
1886 lhs->lhs_dest = current_script_is_vim9() ? dest_script_v9 :
1887 dest_script;
1888
1889 // existing script-local variables should have a type
1890 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
1891 if (import != NULL)
1892 {
1893 if (compile_lhs_import_var(lhs, import, var_start, &var_end,
1894 &rawname) == FAIL)
1895 return FAIL;
1896 }
1897
1898 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
1899 {
1900 // Check writable only when no index follows.
1901 lhs->lhs_scriptvar_idx = get_script_item_idx(
1902 lhs->lhs_scriptvar_sid, rawname,
1903 lhs->lhs_has_index ? ASSIGN_FINAL :
1904 ASSIGN_CONST, cctx, NULL);
1905 if (lhs->lhs_scriptvar_idx >= 0)
1906 {
1907 scriptitem_T *si = SCRIPT_ITEM(lhs->lhs_scriptvar_sid);
1908 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1909 + lhs->lhs_scriptvar_idx;
1910
1911 lhs->lhs_type = sv->sv_type;
1912 }
1913 }
1914
1915 return OK;
1916 }
1917
1918 return check_defined(var_start, lhs->lhs_varlen, cctx, NULL, FALSE);
1919}
1920
1921/*
1922 * Compile the LHS destination.
1923 */
1924 static int
1925compile_lhs_var_dest(
1926 cctx_T *cctx,
1927 lhs_T *lhs,
1928 int cmdidx,
1929 char_u *var_start,
1930 char_u *var_end,
1931 int is_decl)
1932{
1933 int declare_error = FALSE;
1934
1935 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
1936 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
1937 &lhs->lhs_type, cctx) == FAIL)
1938 return FAIL;
1939
1940 if (lhs->lhs_dest != dest_local && cmdidx != CMD_const
1941 && cmdidx != CMD_final)
1942 {
1943 // Specific kind of variable recognized.
1944 declare_error = is_decl;
1945 }
1946 else
1947 {
1948 class_T *defcl;
1949
1950 // No specific kind of variable recognized, just a name.
1951 if (check_reserved_name(lhs->lhs_name, lhs->lhs_has_index
1952 && *var_end == '.') == FAIL)
1953 return FAIL;
1954
1955 if (lookup_local(var_start, lhs->lhs_varlen, &lhs->lhs_local_lvar,
1956 cctx) == OK)
1957 {
1958 lhs->lhs_lvar = &lhs->lhs_local_lvar;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001959 }
1960 else
1961 {
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001962 CLEAR_FIELD(lhs->lhs_arg_lvar);
1963 if (arg_exists(var_start, lhs->lhs_varlen,
1964 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
1965 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001966 {
1967 if (is_decl)
1968 {
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001969 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
Bram Moolenaar752fc692021-01-04 21:57:11 +01001970 return FAIL;
1971 }
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001972 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001973 }
1974 }
1975
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001976 if (lhs->lhs_lvar != NULL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001977 {
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001978 if (is_decl)
1979 {
1980 // if we come here with what looks like an assignment like
1981 // .= but which has been rejected by assignment_len() from
1982 // may_compile_assignment give a better error message
1983 char_u *p = skipwhite(lhs->lhs_end);
1984 if (p[0] == '.' && p[1] == '=')
1985 emsg(_(e_dot_equal_not_supported_with_script_version_two));
1986 else if (p[0] == ':')
1987 // type specified in a non-var assignment
1988 semsg(_(e_trailing_characters_str), p);
1989 else
1990 semsg(_(e_variable_already_declared_str), lhs->lhs_name);
1991 return FAIL;
1992 }
1993 }
1994 else if ((lhs->lhs_classmember_idx = cctx_class_member_idx(
1995 cctx, var_start, lhs->lhs_varlen, &defcl)) >= 0)
1996 {
1997 if (compile_lhs_class_variable(cctx, lhs, defcl, is_decl)
1998 == FAIL)
1999 return FAIL;
2000 }
2001 else
2002 {
2003 if (compile_lhs_script_var(cctx, lhs, var_start, var_end,
2004 is_decl) == FAIL)
2005 return FAIL;
2006 }
2007 }
2008
2009 if (declare_error)
2010 {
2011 vim9_declare_error(lhs->lhs_name);
2012 return FAIL;
2013 }
2014
2015 return OK;
2016}
2017
2018/*
2019 * When compiling a LHS variable name, for a class or an object, set the LHS
2020 * member type.
2021 */
2022 static int
2023compile_lhs_set_oc_member_type(
2024 cctx_T *cctx,
2025 lhs_T *lhs,
2026 char_u *var_start)
2027{
2028 class_T *cl = lhs->lhs_type->tt_class;
2029 int is_object = lhs->lhs_type->tt_type == VAR_OBJECT;
2030 char_u *name = var_start + lhs->lhs_varlen + 1;
2031 size_t namelen = lhs->lhs_end - var_start - lhs->lhs_varlen - 1;
2032
2033 ocmember_T *m = member_lookup(cl, lhs->lhs_type->tt_type,
2034 name, namelen, &lhs->lhs_member_idx);
2035 if (m == NULL)
2036 {
2037 member_not_found_msg(cl, lhs->lhs_type->tt_type, name, namelen);
2038 return FAIL;
2039 }
2040
2041 if (IS_ENUM(cl))
2042 {
2043 if (!inside_class(cctx, cl))
2044 {
2045 semsg(_(e_enumvalue_str_cannot_be_modified),
2046 cl->class_name, m->ocm_name);
Bram Moolenaar752fc692021-01-04 21:57:11 +01002047 return FAIL;
2048 }
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002049 if (lhs->lhs_type->tt_type == VAR_OBJECT &&
2050 lhs->lhs_member_idx < 2)
2051 {
2052 char *msg = lhs->lhs_member_idx == 0 ?
2053 e_enum_str_name_cannot_be_modified :
2054 e_enum_str_ordinal_cannot_be_modified;
2055 semsg(_(msg), cl->class_name);
2056 return FAIL;
2057 }
2058 }
2059
2060 // If it is private member variable, then accessing it outside the
2061 // class is not allowed.
2062 // If it is a read only class variable, then it can be modified
2063 // only inside the class where it is defined.
2064 if ((m->ocm_access != VIM_ACCESS_ALL) &&
2065 ((is_object && !inside_class(cctx, cl))
2066 || (!is_object && cctx->ctx_ufunc->uf_class != cl)))
2067 {
2068 char *msg = (m->ocm_access == VIM_ACCESS_PRIVATE)
2069 ? e_cannot_access_protected_variable_str
2070 : e_variable_is_not_writable_str;
2071 emsg_var_cl_define(msg, m->ocm_name, 0, cl);
2072 return FAIL;
2073 }
2074
2075 if (!IS_CONSTRUCTOR_METHOD(cctx->ctx_ufunc)
2076 && oc_var_check_ro(cl, m))
2077 return FAIL;
2078
2079 lhs->lhs_member_type = m->ocm_type;
2080
2081 return OK;
2082}
2083
2084/*
2085 * When compiling a LHS variable, set the LHS variable type.
2086 */
2087 static int
2088compile_lhs_set_type(cctx_T *cctx, lhs_T *lhs, char_u *var_end, int is_decl)
2089{
2090 if (is_decl && *skipwhite(var_end) == ':')
2091 {
2092 char_u *p;
2093
2094 // parse optional type: "let var: type = expr"
2095 if (VIM_ISWHITE(*var_end))
2096 {
2097 semsg(_(e_no_white_space_allowed_before_colon_str), var_end);
2098 return FAIL;
2099 }
2100
2101 if (!VIM_ISWHITE(var_end[1]))
2102 {
2103 semsg(_(e_white_space_required_after_str_str), ":", var_end);
2104 return FAIL;
2105 }
2106
2107 p = skipwhite(var_end + 1);
2108 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
2109 if (lhs->lhs_type == NULL)
2110 return FAIL;
2111
2112 lhs->lhs_has_type = TRUE;
2113 lhs->lhs_end = p;
2114 }
2115 else if (lhs->lhs_lvar != NULL)
2116 lhs->lhs_type = lhs->lhs_lvar->lv_type;
2117
2118 return OK;
2119}
2120
2121/*
2122 * Returns TRUE if "lhs" is a concatenable string.
2123 */
2124 static int
2125lhs_concatenable(lhs_T *lhs)
2126{
2127 return lhs->lhs_dest == dest_global
2128 || lhs->lhs_has_index
2129 || lhs->lhs_type->tt_type == VAR_STRING
2130 || lhs->lhs_type->tt_type == VAR_ANY;
2131}
2132
2133/*
2134 * Create a new local variable when compiling a LHS variable.
2135 */
2136 static int
2137compile_lhs_new_local_var(
2138 cctx_T *cctx,
2139 lhs_T *lhs,
2140 char_u *var_start,
2141 int cmdidx,
2142 int oplen,
2143 int is_decl,
2144 int has_cmd,
2145 int heredoc)
2146{
2147 if (oplen > 1 && !heredoc)
2148 {
2149 // +=, /=, etc. require an existing variable
2150 semsg(_(e_cannot_use_operator_on_new_variable_str), lhs->lhs_name);
2151 return FAIL;
2152 }
2153
2154 if (!is_decl || (lhs->lhs_has_index && !has_cmd
2155 && cctx->ctx_skip != SKIP_YES))
2156 {
2157 semsg(_(e_unknown_variable_str), lhs->lhs_name);
2158 return FAIL;
2159 }
2160
2161 // Check the name is valid for a funcref.
2162 if (lhs->lhs_type->tt_type == VAR_FUNC
2163 || lhs->lhs_type->tt_type == VAR_PARTIAL)
2164 {
2165 if (var_wrong_func_name(lhs->lhs_name, TRUE))
2166 return FAIL;
2167 }
2168
2169 // New local variable.
2170 int assign;
2171 switch (cmdidx)
2172 {
2173 case CMD_final:
2174 assign = ASSIGN_FINAL; break;
2175 case CMD_const:
2176 assign = ASSIGN_CONST; break;
2177 default:
2178 assign = ASSIGN_VAR; break;
2179 }
2180
2181 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen, assign,
2182 lhs->lhs_type);
2183 if (lhs->lhs_lvar == NULL)
2184 return FAIL;
2185
2186 lhs->lhs_new_local = TRUE;
2187
2188 return OK;
2189}
2190
2191/*
2192 * When compiling a LHS variable name, set the LHS member type.
2193 */
2194 static int
2195compile_lhs_set_member_type(
2196 cctx_T *cctx,
2197 lhs_T *lhs,
2198 char_u *var_start,
2199 int is_decl,
2200 int has_cmd)
2201{
2202 lhs->lhs_member_type = lhs->lhs_type;
2203
2204 if (!lhs->lhs_has_index)
2205 return OK;
2206
2207 char_u *after = var_start + lhs->lhs_varlen;
2208 char_u *p;
2209
2210 // Something follows after the variable: "var[idx]" or "var.key".
2211 if (is_decl && cctx->ctx_skip != SKIP_YES)
2212 {
2213 if (has_cmd)
2214 emsg(_(e_cannot_use_index_when_declaring_variable));
2215 else
2216 semsg(_(e_unknown_variable_str), lhs->lhs_name);
2217 return FAIL;
2218 }
2219
2220 // Now: var_start[lhs->lhs_varlen] is '[' or '.'
2221 // Only the last index is used below, if there are others
2222 // before it generate code for the expression. Thus for
2223 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
2224 for (;;)
2225 {
2226 p = skip_index(after);
2227 if (*p != '[' && *p != '.')
2228 {
2229 lhs->lhs_varlen_total = p - var_start;
2230 break;
2231 }
2232 after = p;
2233 }
2234 if (after > var_start + lhs->lhs_varlen)
2235 {
2236 lhs->lhs_varlen = after - var_start;
2237 lhs->lhs_dest = dest_expr;
2238 // We don't know the type before evaluating the expression,
2239 // use "any" until then.
2240 lhs->lhs_type = &t_any;
2241 }
2242
2243 int use_class = lhs->lhs_type != NULL
2244 && (lhs->lhs_type->tt_type == VAR_CLASS
2245 || lhs->lhs_type->tt_type == VAR_OBJECT);
2246
2247 if (lhs->lhs_type == NULL
2248 || (use_class ? lhs->lhs_type->tt_class == NULL
2249 : lhs->lhs_type->tt_member == NULL))
2250 {
2251 lhs->lhs_member_type = &t_any;
2252 }
2253 else if (use_class)
2254 {
2255 // for an object or class member get the type of the member
2256 if (compile_lhs_set_oc_member_type(cctx, lhs, var_start) == FAIL)
2257 return FAIL;
2258 }
2259 else
2260 lhs->lhs_member_type = lhs->lhs_type->tt_member;
2261
2262 return OK;
2263}
2264
2265/*
2266 * Figure out the LHS type and other properties for an assignment or one item
2267 * of ":unlet" with an index.
2268 * Returns OK or FAIL.
2269 */
2270 int
2271compile_lhs(
2272 char_u *var_start,
2273 lhs_T *lhs,
2274 cmdidx_T cmdidx,
2275 int heredoc,
2276 int has_cmd, // "var" before "var_start"
2277 int oplen,
2278 cctx_T *cctx)
2279{
Yegappan Lakshmanan59c88802024-12-19 20:00:31 +01002280 char_u *var_end = NULL;
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002281 int is_decl = is_decl_command(cmdidx);
2282
2283 if (lhs_init(lhs, var_start, is_decl, heredoc, &var_end) == FAIL)
2284 return FAIL;
2285
2286 if (cctx->ctx_skip != SKIP_YES)
2287 {
2288 // compile the LHS destination
2289 if (compile_lhs_var_dest(cctx, lhs, cmdidx, var_start, var_end,
2290 is_decl) == FAIL)
2291 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002292 }
2293
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00002294 // handle "a:name" as a name, not index "name" in "a"
Bram Moolenaar752fc692021-01-04 21:57:11 +01002295 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
2296 var_end = lhs->lhs_dest_end;
2297
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002298 if (lhs->lhs_dest != dest_option && lhs->lhs_dest != dest_func_option)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002299 {
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002300 // set the LHS variable type
2301 if (compile_lhs_set_type(cctx, lhs, var_end, is_decl) == FAIL)
2302 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002303 }
2304
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002305 if (oplen == 3 && !heredoc && !lhs_concatenable(lhs))
Bram Moolenaar752fc692021-01-04 21:57:11 +01002306 {
2307 emsg(_(e_can_only_concatenate_to_string));
2308 return FAIL;
2309 }
2310
2311 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002312 && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002313 {
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002314 if (compile_lhs_new_local_var(cctx, lhs, var_start, cmdidx, oplen,
2315 is_decl, has_cmd, heredoc) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002316 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002317 }
2318
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002319 if (compile_lhs_set_member_type(cctx, lhs, var_start, is_decl, has_cmd)
2320 == FAIL)
2321 return FAIL;
Bram Moolenaarc750d912021-11-29 22:02:12 +00002322
Bram Moolenaar752fc692021-01-04 21:57:11 +01002323 return OK;
2324}
2325
2326/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002327 * Figure out the LHS and check a few errors.
2328 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002329 int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002330compile_assign_lhs(
Bram Moolenaar97f8c102022-04-02 19:43:57 +01002331 char_u *var_start,
2332 lhs_T *lhs,
2333 cmdidx_T cmdidx,
2334 int is_decl,
2335 int heredoc,
2336 int has_cmd, // "var" before "var_start"
2337 int oplen,
2338 cctx_T *cctx)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002339{
Bram Moolenaar97f8c102022-04-02 19:43:57 +01002340 if (compile_lhs(var_start, lhs, cmdidx, heredoc, has_cmd, oplen, cctx)
2341 == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002342 return FAIL;
2343
2344 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
2345 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002346 semsg(_(e_cannot_assign_to_argument_str), lhs->lhs_name);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002347 return FAIL;
2348 }
2349 if (!is_decl && lhs->lhs_lvar != NULL
Bram Moolenaar6586a012022-09-30 11:04:50 +01002350 && lhs->lhs_lvar->lv_const != ASSIGN_VAR
2351 && !lhs->lhs_has_index)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002352 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002353 semsg(_(e_cannot_assign_to_constant_str), lhs->lhs_name);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002354 return FAIL;
2355 }
2356 return OK;
2357}
2358
2359/*
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002360 * Return TRUE if "lhs" has a range index: "[expr : expr]".
2361 */
2362 static int
2363has_list_index(char_u *idx_start, cctx_T *cctx)
2364{
2365 char_u *p = idx_start;
2366 int save_skip;
2367
2368 if (*p != '[')
2369 return FALSE;
2370
2371 p = skipwhite(p + 1);
2372 if (*p == ':')
2373 return TRUE;
2374
2375 save_skip = cctx->ctx_skip;
2376 cctx->ctx_skip = SKIP_YES;
2377 (void)compile_expr0(&p, cctx);
2378 cctx->ctx_skip = save_skip;
2379 return *skipwhite(p) == ':';
2380}
2381
2382/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002383 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
2384 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01002385 */
2386 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02002387compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01002388 char_u *var_start,
2389 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002390 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01002391 cctx_T *cctx)
2392{
Bram Moolenaar752fc692021-01-04 21:57:11 +01002393 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002394 char_u *p;
2395 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02002396 int need_white_before = TRUE;
2397 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002398
Bram Moolenaar752fc692021-01-04 21:57:11 +01002399 p = var_start + varlen;
2400 if (*p == '[')
2401 {
2402 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02002403 if (*p == ':')
2404 {
2405 // empty first index, push zero
2406 r = generate_PUSHNR(cctx, 0);
2407 need_white_before = FALSE;
2408 }
2409 else
2410 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002411
2412 if (r == OK && *skipwhite(p) == ':')
2413 {
2414 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02002415 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02002416 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002417 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02002418 empty_second = *skipwhite(p + 1) == ']';
2419 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
2420 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002421 {
2422 semsg(_(e_white_space_required_before_and_after_str_at_str),
2423 ":", p);
2424 return FAIL;
2425 }
2426 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02002427 if (*p == ']')
2428 // empty second index, push "none"
2429 r = generate_PUSHSPEC(cctx, VVAL_NONE);
2430 else
2431 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002432 }
2433
Bram Moolenaar752fc692021-01-04 21:57:11 +01002434 if (r == OK && *skipwhite(p) != ']')
2435 {
2436 // this should not happen
Bram Moolenaare1242042021-12-16 20:56:57 +00002437 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar752fc692021-01-04 21:57:11 +01002438 r = FAIL;
2439 }
2440 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002441 else if (lhs->lhs_member_idx >= 0)
2442 {
2443 // object member index
2444 r = generate_PUSHNR(cctx, lhs->lhs_member_idx);
2445 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002446 else // if (*p == '.')
2447 {
2448 char_u *key_end = to_name_end(p + 1, TRUE);
2449 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
2450
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02002451 r = generate_PUSHS(cctx, &key);
Bram Moolenaar752fc692021-01-04 21:57:11 +01002452 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02002453 return r;
2454}
2455
2456/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002457 * For a LHS with an index, load the variable to be indexed.
2458 */
2459 static int
2460compile_load_lhs(
2461 lhs_T *lhs,
2462 char_u *var_start,
2463 type_T *rhs_type,
2464 cctx_T *cctx)
2465{
2466 if (lhs->lhs_dest == dest_expr)
2467 {
2468 size_t varlen = lhs->lhs_varlen;
2469 int c = var_start[varlen];
Bram Moolenaare97976b2021-08-01 13:17:17 +02002470 int lines_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaare97976b2021-08-01 13:17:17 +02002471 int res;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002472
Bram Moolenaare97976b2021-08-01 13:17:17 +02002473 // Evaluate "ll[expr]" of "ll[expr][idx]". End the line with a NUL and
2474 // limit the lines array length to avoid skipping to a following line.
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002475 var_start[varlen] = NUL;
Bram Moolenaare97976b2021-08-01 13:17:17 +02002476 cctx->ctx_ufunc->uf_lines.ga_len = cctx->ctx_lnum + 1;
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002477 char_u *p = var_start;
Bram Moolenaare97976b2021-08-01 13:17:17 +02002478 res = compile_expr0(&p, cctx);
2479 var_start[varlen] = c;
2480 cctx->ctx_ufunc->uf_lines.ga_len = lines_len;
2481 if (res == FAIL || p != var_start + varlen)
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002482 {
2483 // this should not happen
Bram Moolenaare97976b2021-08-01 13:17:17 +02002484 if (res != FAIL)
Bram Moolenaare1242042021-12-16 20:56:57 +00002485 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002486 return FAIL;
2487 }
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002488
Bram Moolenaar078a4612022-01-04 15:17:03 +00002489 lhs->lhs_type = cctx->ctx_type_stack.ga_len == 0 ? &t_void
2490 : get_type_on_stack(cctx, 0);
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02002491
Yegappan Lakshmanan54d7f182025-02-10 21:35:07 +01002492 if (lhs->lhs_type->tt_type == VAR_CLASS
Yegappan Lakshmanande8f8f72025-04-01 20:43:36 +02002493 || (lhs->lhs_type->tt_type == VAR_OBJECT
2494 && lhs->lhs_type != &t_object_any))
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02002495 {
Yegappan Lakshmanan54d7f182025-02-10 21:35:07 +01002496 // Check whether the class or object variable is modifiable
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02002497 if (!lhs_class_member_modifiable(lhs, var_start, cctx))
2498 return FAIL;
2499 }
2500
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002501 // Now we can properly check the type. The variable is indexed, thus
2502 // we need the member type. For a class or object we don't know the
2503 // type yet, it depends on what member is used.
Yegappan Lakshmananc229a6a2023-10-26 23:05:07 +02002504 // The top item in the stack is the Dict, followed by the key and then
2505 // the type of the value.
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002506 vartype_T vartype = lhs->lhs_type->tt_type;
2507 type_T *member_type = lhs->lhs_type->tt_member;
2508 if (rhs_type != NULL && member_type != NULL
2509 && vartype != VAR_OBJECT && vartype != VAR_CLASS
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002510 && rhs_type != &t_void
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002511 && need_type(rhs_type, member_type, FALSE,
Yegappan Lakshmananc229a6a2023-10-26 23:05:07 +02002512 -3, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002513 return FAIL;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002514
2515 return OK;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002516 }
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002517
Yegappan Lakshmanande8f8f72025-04-01 20:43:36 +02002518 return generate_loadvar(cctx, lhs);
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002519}
2520
2521/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02002522 * Produce code for loading "lhs" and also take care of an index.
2523 * Return OK/FAIL.
2524 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002525 int
Bram Moolenaara369c3d2021-04-21 16:00:10 +02002526compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
2527{
Bram Moolenaar4cae8452023-01-15 15:51:48 +00002528 if (lhs->lhs_type->tt_type == VAR_OBJECT)
2529 {
Bram Moolenaar22363c62023-04-24 17:15:25 +01002530 // "this.value": load "this" object and get the value at index for an
2531 // object or class member get the type of the member.
2532 // Also for "obj.value".
Yegappan Lakshmananff6f0d52023-12-21 16:46:18 +01002533 char_u *dot = vim_strchr(var_start, '.');
2534 if (dot == NULL)
2535 {
2536 semsg(_(e_missing_dot_after_object_str), lhs->lhs_name);
2537 return FAIL;
2538 }
Bram Moolenaar22363c62023-04-24 17:15:25 +01002539
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +02002540 class_T *cl = lhs->lhs_type->tt_class;
Yegappan Lakshmanan1ea42882023-10-11 21:43:52 +02002541 type_T *type = oc_member_type(cl, TRUE, dot + 1,
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02002542 lhs->lhs_end, &lhs->lhs_member_idx);
Bram Moolenaar4cae8452023-01-15 15:51:48 +00002543 if (lhs->lhs_member_idx < 0)
2544 return FAIL;
2545
Bram Moolenaar22363c62023-04-24 17:15:25 +01002546 if (dot - var_start == 4 && STRNCMP(var_start, "this", 4) == 0)
2547 {
2548 // load "this"
Yegappan Lakshmanand990bf02024-03-22 19:56:17 +01002549 lvar_T *lvar = lhs->lhs_lvar;
2550 int rc;
2551
2552 if (lvar->lv_from_outer > 0)
2553 rc = generate_LOADOUTER(cctx, lvar->lv_idx,
2554 lvar->lv_from_outer, lvar->lv_loop_depth,
2555 lvar->lv_loop_idx, type);
2556 else
2557 rc = generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
2558
2559 if (rc == FAIL)
Bram Moolenaar22363c62023-04-24 17:15:25 +01002560 return FAIL;
2561 }
2562 else
2563 {
2564 // load object variable or argument
2565 if (compile_load_lhs(lhs, var_start, lhs->lhs_type, cctx) == FAIL)
2566 return FAIL;
2567 }
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01002568 if (IS_INTERFACE(cl))
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02002569 return generate_GET_ITF_MEMBER(cctx, cl, lhs->lhs_member_idx, type);
2570 return generate_GET_OBJ_MEMBER(cctx, lhs->lhs_member_idx, type);
Bram Moolenaar4cae8452023-01-15 15:51:48 +00002571 }
Yegappan Lakshmanan1ea42882023-10-11 21:43:52 +02002572 else if (lhs->lhs_type->tt_type == VAR_CLASS)
2573 {
2574 // "<classname>.value": load class variable "classname.value"
Yegappan Lakshmananff6f0d52023-12-21 16:46:18 +01002575 char_u *dot = vim_strchr(var_start, '.');
2576 if (dot == NULL)
2577 {
2578 check_type_is_value(lhs->lhs_type);
2579 return FAIL;
2580 }
Yegappan Lakshmanan1ea42882023-10-11 21:43:52 +02002581
2582 class_T *cl = lhs->lhs_type->tt_class;
2583 ocmember_T *m = class_member_lookup(cl, dot + 1,
2584 lhs->lhs_end - dot - 1,
2585 &lhs->lhs_member_idx);
2586 if (m == NULL)
2587 return FAIL;
2588
2589 return generate_CLASSMEMBER(cctx, TRUE, cl, lhs->lhs_member_idx);
2590 }
Bram Moolenaar4cae8452023-01-15 15:51:48 +00002591
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002592 if (compile_load_lhs(lhs, var_start, NULL, cctx) == FAIL)
2593 return FAIL;
Bram Moolenaara369c3d2021-04-21 16:00:10 +02002594
2595 if (lhs->lhs_has_index)
2596 {
2597 int range = FALSE;
2598
2599 // Get member from list or dict. First compile the
2600 // index value.
2601 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
2602 return FAIL;
2603 if (range)
2604 {
2605 semsg(_(e_cannot_use_range_with_assignment_operator_str),
2606 var_start);
2607 return FAIL;
2608 }
2609
2610 // Get the member.
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002611 if (compile_member(FALSE, NULL, cctx) == FAIL)
Bram Moolenaara369c3d2021-04-21 16:00:10 +02002612 return FAIL;
2613 }
2614 return OK;
2615}
2616
2617/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002618 * Assignment to a list or dict member, or ":unlet" for the item, using the
2619 * information in "lhs".
2620 * Returns OK or FAIL.
2621 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002622 int
Bram Moolenaare42939a2021-04-05 17:11:17 +02002623compile_assign_unlet(
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002624 char_u *var_start,
2625 lhs_T *lhs,
2626 int is_assign,
2627 type_T *rhs_type,
2628 cctx_T *cctx)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002629{
Bram Moolenaare42939a2021-04-05 17:11:17 +02002630 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002631 int range = FALSE;
2632
Bram Moolenaar68452172021-04-12 21:21:02 +02002633 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002634 return FAIL;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002635 if (is_assign && range
2636 && lhs->lhs_type->tt_type != VAR_LIST
2637 && lhs->lhs_type != &t_blob
2638 && lhs->lhs_type != &t_any)
Bram Moolenaar68452172021-04-12 21:21:02 +02002639 {
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002640 if (lhs->lhs_type->tt_type == VAR_TUPLE)
2641 emsg(_(e_cannot_slice_tuple));
2642 else
2643 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
Bram Moolenaar68452172021-04-12 21:21:02 +02002644 return FAIL;
2645 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002646
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002647 if (lhs->lhs_type == NULL || lhs->lhs_type == &t_any)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002648 {
2649 // Index on variable of unknown type: check at runtime.
2650 dest_type = VAR_ANY;
2651 }
2652 else
2653 {
2654 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002655 if (dest_type == VAR_DICT && range)
2656 {
zeertzjq276410e2023-05-07 21:59:33 +01002657 emsg(_(e_cannot_use_range_with_dictionary));
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002658 return FAIL;
2659 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002660 if (dest_type == VAR_DICT
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02002661 && may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002662 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02002663 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002664 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002665 type_T *type;
2666
2667 if (range)
2668 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00002669 type = get_type_on_stack(cctx, 1);
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002670 if (need_type(type, &t_number, FALSE,
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002671 -2, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002672 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02002673 }
Bram Moolenaar078a4612022-01-04 15:17:03 +00002674 type = get_type_on_stack(cctx, 0);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00002675 if ((dest_type != VAR_BLOB && type->tt_type != VAR_SPECIAL)
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002676 && need_type(type, &t_number, FALSE,
Bram Moolenaar51e93322021-04-17 20:44:56 +02002677 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002678 return FAIL;
2679 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002680 }
2681
Bram Moolenaar4875d6a2022-08-17 15:55:51 +01002682 if (cctx->ctx_skip == SKIP_YES)
2683 return OK;
2684
Bram Moolenaar22363c62023-04-24 17:15:25 +01002685 // Load the dict, list or object. On the stack we then have:
Bram Moolenaar752fc692021-01-04 21:57:11 +01002686 // - value (for assignment, not for :unlet)
2687 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002688 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01002689 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002690 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
2691 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002692
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002693 if (dest_type == VAR_LIST
2694 || dest_type == VAR_DICT
2695 || dest_type == VAR_BLOB
2696 || dest_type == VAR_CLASS
2697 || dest_type == VAR_OBJECT
2698 || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002699 {
2700 if (is_assign)
2701 {
Bram Moolenaar68452172021-04-12 21:21:02 +02002702 if (range)
2703 {
2704 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
2705 return FAIL;
2706 }
2707 else
2708 {
2709 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01002710
Bram Moolenaar68452172021-04-12 21:21:02 +02002711 if (isn == NULL)
2712 return FAIL;
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002713 isn->isn_arg.storeindex.si_vartype = dest_type;
2714 isn->isn_arg.storeindex.si_class = NULL;
2715
2716 if (dest_type == VAR_OBJECT)
2717 {
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +00002718 class_T *cl = lhs->lhs_type->tt_class;
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002719
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01002720 if (IS_INTERFACE(cl))
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002721 {
2722 // "this.value": load "this" object and get the value
2723 // at index for an object or class member get the type
2724 // of the member
2725 isn->isn_arg.storeindex.si_class = cl;
2726 ++cl->class_refcount;
2727 }
2728 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002729 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002730 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002731 else if (range)
2732 {
2733 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
2734 return FAIL;
2735 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002736 else
2737 {
2738 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
2739 return FAIL;
2740 }
2741 }
2742 else
2743 {
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002744 if (dest_type == VAR_TUPLE)
2745 emsg(_(e_tuple_is_immutable));
2746 else
2747 emsg(_(e_indexable_type_required));
Bram Moolenaar752fc692021-01-04 21:57:11 +01002748 return FAIL;
2749 }
2750
2751 return OK;
2752}
2753
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002754/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002755 * Generate an instruction to push the default value for "vartype".
2756 * if "dest_local" is TRUE then for some types no instruction is generated.
2757 * "skip_store" is set to TRUE if no PUSH instruction is generated.
2758 * Returns OK or FAIL.
2759 */
2760 static int
2761push_default_value(
2762 cctx_T *cctx,
2763 vartype_T vartype,
2764 int dest_is_local,
2765 int *skip_store)
2766{
2767 int r = OK;
2768
2769 switch (vartype)
2770 {
2771 case VAR_BOOL:
2772 r = generate_PUSHBOOL(cctx, VVAL_FALSE);
2773 break;
2774 case VAR_FLOAT:
2775 r = generate_PUSHF(cctx, 0.0);
2776 break;
2777 case VAR_STRING:
2778 r = generate_PUSHS(cctx, NULL);
2779 break;
2780 case VAR_BLOB:
2781 r = generate_PUSHBLOB(cctx, blob_alloc());
2782 break;
2783 case VAR_FUNC:
2784 r = generate_PUSHFUNC(cctx, NULL, &t_func_void, TRUE);
2785 break;
2786 case VAR_LIST:
2787 r = generate_NEWLIST(cctx, 0, FALSE);
2788 break;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002789 case VAR_TUPLE:
2790 r = generate_NEWTUPLE(cctx, 0, FALSE);
2791 break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002792 case VAR_DICT:
2793 r = generate_NEWDICT(cctx, 0, FALSE);
2794 break;
2795 case VAR_JOB:
2796 r = generate_PUSHJOB(cctx);
2797 break;
2798 case VAR_CHANNEL:
2799 r = generate_PUSHCHANNEL(cctx);
2800 break;
Ernie Rael5c018be2023-08-27 18:40:26 +02002801 case VAR_OBJECT:
2802 r = generate_PUSHOBJ(cctx);
2803 break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002804 case VAR_NUMBER:
2805 case VAR_UNKNOWN:
2806 case VAR_ANY:
2807 case VAR_PARTIAL:
2808 case VAR_VOID:
2809 case VAR_INSTR:
2810 case VAR_CLASS:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002811 case VAR_TYPEALIAS:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002812 case VAR_SPECIAL: // cannot happen
2813 // This is skipped for local variables, they are always
2814 // initialized to zero. But in a "for" or "while" loop
2815 // the value may have been changed.
2816 if (dest_is_local && !inside_loop_scope(cctx))
2817 *skip_store = TRUE;
2818 else
2819 r = generate_PUSHNR(cctx, 0);
2820 break;
2821 }
2822 return r;
2823}
2824
2825/*
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002826 * Compile assignment context. Used when compiling an assignment statement.
2827 */
2828typedef struct cac_S cac_T;
2829struct cac_S
2830{
2831 cmdidx_T cac_cmdidx; // assignment command
2832 char_u *cac_nextc; // next character to parse
2833 lhs_T cac_lhs; // lhs of the assignment
2834 type_T *cac_rhs_type; // rhs type of an assignment
2835 char_u *cac_op; // assignment operator
2836 int cac_oplen; // assignment operator length
2837 char_u *cac_var_start; // start of the variable names
2838 char_u *cac_var_end; // end of the variable names
2839 int cac_var_count; // number of variables in assignment
2840 int cac_var_idx; // variable index in a list
2841 int cac_semicolon; // semicolon in [var1, var2; var3]
2842 garray_T *cac_instr;
2843 int cac_instr_count;
2844 int cac_incdec;
2845 int cac_did_generate_slice;
2846 int cac_is_decl;
2847 int cac_is_const;
2848 int cac_start_lnum;
2849 type_T *cac_inferred_type;
2850 int cac_skip_store;
2851};
2852
2853/*
2854 * Initialize the compile assignment context.
2855 */
2856 static void
2857compile_assign_context_init(cac_T *cac, cctx_T *cctx, int cmdidx, char_u *arg)
2858{
2859 CLEAR_FIELD(*cac);
2860 cac->cac_cmdidx = cmdidx;
2861 cac->cac_instr = &cctx->ctx_instr;
2862 cac->cac_rhs_type = &t_any;
2863 cac->cac_is_decl = is_decl_command(cmdidx);
2864 cac->cac_start_lnum = SOURCING_LNUM;
2865 cac->cac_instr_count = -1;
2866 cac->cac_var_end = arg;
2867}
2868
2869/*
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01002870 * Compile an object member variable assignment in the arguments passed to a
2871 * class new() method.
2872 *
2873 * Instruction format:
2874 *
2875 * ifargisset <n> this.<varname> = <value>
2876 *
2877 * where <n> is the index of the default argument.
2878 *
2879 * Generates the ISN_JUMP_IF_ARG_NOT_SET instruction to skip the assignment if
2880 * the value is passed as an argument to the new() method call.
2881 *
2882 * Returns OK on success.
2883 */
2884 static int
2885compile_assign_obj_new_arg(char_u **argp, cctx_T *cctx)
2886{
2887 char_u *arg = *argp;
2888
2889 arg += 11; // skip "ifargisset"
2890 int def_arg_idx = getdigits(&arg);
2891 arg = skipwhite(arg);
2892
2893 // Use a JUMP_IF_ARG_NOT_SET instruction to skip if the value was not
2894 // given and the default value is "v:none".
2895 int stack_offset = STACK_FRAME_SIZE +
2896 (cctx->ctx_ufunc->uf_va_name != NULL ? 1 : 0);
2897 int def_arg_count = cctx->ctx_ufunc->uf_def_args.ga_len;
2898 int arg_offset = def_arg_idx - def_arg_count - stack_offset;
2899
2900 if (generate_JUMP_IF_ARG(cctx, ISN_JUMP_IF_ARG_NOT_SET,
2901 arg_offset) == FAIL)
2902 return FAIL;
2903
2904 *argp = arg;
2905 return OK;
2906}
2907
2908/*
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002909 * Translate the increment (++) and decrement (--) operators to the
2910 * corresponding compound operators (+= or -=).
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01002911 *
2912 * Returns OK on success and FAIL on syntax error.
2913 */
2914 static int
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002915translate_incdec_op(exarg_T *eap, cac_T *cac)
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01002916{
2917 if (VIM_ISWHITE(eap->cmd[2]))
2918 {
2919 semsg(_(e_no_white_space_allowed_after_str_str),
2920 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
2921 return FAIL;
2922 }
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002923 cac->cac_op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
2924 cac->cac_oplen = 2;
2925 cac->cac_incdec = TRUE;
2926
2927 return OK;
2928}
2929
2930/*
2931 * Process the operator in an assignment statement.
2932 */
2933 static int
2934compile_assign_process_operator(
2935 exarg_T *eap,
2936 char_u *arg,
2937 cac_T *cac,
2938 int *heredoc,
2939 char_u **retstr)
2940{
2941 *retstr = NULL;
2942
2943 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002944 // Change an unary operator to a compound operator
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01002945 return translate_incdec_op(eap, cac);
2946
2947 char_u *sp = cac->cac_nextc;
2948 cac->cac_nextc = skipwhite(cac->cac_nextc);
2949 cac->cac_op = cac->cac_nextc;
2950 cac->cac_oplen = assignment_len(cac->cac_nextc, heredoc);
2951
2952 if (cac->cac_var_count > 0 && cac->cac_oplen == 0)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002953 {
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01002954 // can be something like "[1, 2]->func()"
2955 *retstr = arg;
2956 return FAIL;
2957 }
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002958
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01002959 // need white space before and after the operator
2960 if (cac->cac_oplen > 0 && (!VIM_ISWHITE(*sp)
2961 || !IS_WHITE_OR_NUL(cac->cac_op[cac->cac_oplen])))
2962 {
2963 error_white_both(cac->cac_op, cac->cac_oplen);
2964 return FAIL;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002965 }
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01002966
2967 return OK;
2968}
2969
2970/*
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01002971 * Find the start of an assignment statement.
2972 */
2973 static char_u *
2974compile_assign_compute_start(char_u *arg, int var_count)
2975{
2976 if (var_count > 0)
2977 // [var1, var2] = [val1, val2]
2978 // skip over the "["
2979 return skipwhite(arg + 1);
2980
2981 return arg;
2982}
2983
2984/*
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01002985 * Parse a heredoc assignment starting at "p". Returns a pointer to the
2986 * beginning of the heredoc content.
2987 */
2988 static char_u *
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002989parse_heredoc_assignment(exarg_T *eap, cctx_T *cctx, cac_T *cac)
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01002990{
2991 // [let] varname =<< [trim] {end}
2992 eap->ea_getline = exarg_getline;
2993 eap->cookie = cctx;
2994
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002995 list_T *l = heredoc_get(eap, cac->cac_nextc + 3, FALSE, TRUE);
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01002996 if (l == NULL)
2997 return NULL;
2998
2999 list_free(l);
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003000 cac->cac_nextc += STRLEN(cac->cac_nextc);
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003001
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003002 return cac->cac_nextc;
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003003}
3004
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003005/*
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003006 * Check the type of a RHS expression in a list assignment statement.
3007 * The RHS expression is already compiled. So the type is on the stack.
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003008 */
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003009 static int
3010compile_assign_list_check_rhs_type(cctx_T *cctx, cac_T *cac)
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003011{
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003012 type_T *stacktype;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003013
3014 stacktype = cctx->ctx_type_stack.ga_len == 0 ? &t_void
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003015 : get_type_on_stack(cctx, 0);
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003016 if (stacktype->tt_type == VAR_VOID)
3017 {
3018 emsg(_(e_cannot_use_void_value));
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003019 return FAIL;
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003020 }
3021
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01003022 if (stacktype->tt_type != VAR_LIST && stacktype->tt_type != VAR_TUPLE
3023 && stacktype->tt_type != VAR_ANY)
3024 {
3025 emsg(_(e_list_or_tuple_required));
3026 return FAIL;
3027 }
3028
3029 if (need_type(stacktype,
3030 stacktype->tt_type == VAR_TUPLE ? &t_tuple_any : &t_list_any,
3031 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003032 return FAIL;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003033
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01003034 if (stacktype->tt_type == VAR_TUPLE)
3035 {
3036 if (stacktype->tt_argcount != 1)
3037 cac->cac_rhs_type = &t_any;
3038 else
3039 {
3040 if (stacktype->tt_flags & TTFLAG_VARARGS)
3041 cac->cac_rhs_type = stacktype->tt_args[0]->tt_member;
3042 else
3043 cac->cac_rhs_type = stacktype->tt_args[0];
3044 }
3045 }
3046 else if (stacktype->tt_member != NULL)
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003047 cac->cac_rhs_type = stacktype->tt_member;
3048
3049 return OK;
3050}
3051
3052/*
3053 * In a list assignment statement, if a constant list was used, check the
3054 * length. Returns OK if the length check succeeds. Returns FAIL otherwise.
3055 */
3056 static int
3057compile_assign_list_check_length(cctx_T *cctx, cac_T *cac)
3058{
3059 int needed_list_len;
3060 int did_check = FALSE;
3061
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003062 needed_list_len = cac->cac_semicolon
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003063 ? cac->cac_var_count - 1
3064 : cac->cac_var_count;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003065 if (cac->cac_instr->ga_len > 0)
3066 {
3067 isn_T *isn = ((isn_T *)cac->cac_instr->ga_data) +
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003068 cac->cac_instr->ga_len - 1;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003069
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01003070 if (isn->isn_type == ISN_NEWLIST || isn->isn_type == ISN_NEWTUPLE)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003071 {
3072 did_check = TRUE;
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003073 if (cac->cac_semicolon ?
3074 isn->isn_arg.number < needed_list_len
3075 : isn->isn_arg.number != needed_list_len)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003076 {
3077 semsg(_(e_expected_nr_items_but_got_nr),
3078 needed_list_len, (int)isn->isn_arg.number);
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003079 return FAIL;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003080 }
3081 }
3082 }
3083
3084 if (!did_check)
3085 generate_CHECKLEN(cctx, needed_list_len, cac->cac_semicolon);
3086
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003087 return OK;
3088}
3089
3090/*
3091 * Evaluate the expression for "[var, var] = expr" assignment.
3092 * A line break may follow the assignment operator "=".
3093 */
3094 static char_u *
3095compile_assign_list_expr(cctx_T *cctx, cac_T *cac)
3096{
3097 char_u *whitep;
3098
3099 whitep = cac->cac_op + cac->cac_oplen;
3100
3101 if (may_get_next_line_error(whitep, &cac->cac_nextc, cctx) == FAIL)
3102 return NULL;
3103
3104 // compile RHS expression
3105 if (compile_expr0(&cac->cac_nextc, cctx) == FAIL)
3106 return NULL;
3107
3108 if (cctx->ctx_skip == SKIP_YES)
3109 // no need to parse more when skipping
3110 return cac->cac_nextc;
3111
3112 if (compile_assign_list_check_rhs_type(cctx, cac) == FAIL)
3113 return NULL;
3114
3115 // If a constant list was used we can check the length right here.
3116 if (compile_assign_list_check_length(cctx, cac) == FAIL)
3117 return FAIL;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003118
3119 return cac->cac_nextc;
3120}
3121
3122/*
3123 * Find and return the end of a heredoc or a list of variables assignment
3124 * statement. For a single variable assignment statement, returns the current
3125 * end.
3126 * Returns NULL on failure.
3127 */
3128 static char_u *
3129compile_assign_compute_end(
3130 exarg_T *eap,
3131 cctx_T *cctx,
3132 cac_T *cac,
3133 int heredoc)
3134{
3135 if (heredoc)
3136 {
3137 cac->cac_nextc = parse_heredoc_assignment(eap, cctx, cac);
3138 return cac->cac_nextc;
3139 }
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003140
3141 if (cac->cac_var_count > 0)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003142 {
3143 // for "[var, var] = expr" evaluate the expression. The list of
3144 // variables are processed later.
3145 // A line break may follow the "=".
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003146 cac->cac_nextc = compile_assign_list_expr(cctx, cac);
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003147 return cac->cac_nextc;
3148 }
3149
3150 return cac->cac_var_end;
3151}
3152
3153/*
3154 * For "var = expr" evaluate the expression.
3155 */
3156 static int
3157compile_assign_single_eval_expr(cctx_T *cctx, cac_T *cac)
3158{
3159 int ret = OK;
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003160 char_u *whitep;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003161 lhs_T *lhs = &cac->cac_lhs;
3162
3163 // Compile the expression.
3164 if (cac->cac_incdec)
3165 return generate_PUSHNR(cctx, 1);
3166
3167 // Temporarily hide the new local variable here, it is
3168 // not available to this expression.
3169 if (lhs->lhs_new_local)
3170 --cctx->ctx_locals.ga_len;
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003171 whitep = cac->cac_op + cac->cac_oplen;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003172
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003173 if (may_get_next_line_error(whitep, &cac->cac_nextc, cctx) == FAIL)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003174 {
3175 if (lhs->lhs_new_local)
3176 ++cctx->ctx_locals.ga_len;
3177 return FAIL;
3178 }
3179
3180 ret = compile_expr0_ext(&cac->cac_nextc, cctx, &cac->cac_is_const);
3181 if (lhs->lhs_new_local)
3182 ++cctx->ctx_locals.ga_len;
3183
3184 return ret;
3185}
3186
3187/*
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003188 * When compiling an assignment, set the LHS type to the RHS type.
3189 */
3190 static int
3191compile_assign_set_lhs_type_from_rhs(
3192 cctx_T *cctx,
3193 cac_T *cac,
3194 lhs_T *lhs,
3195 type_T *rhs_type)
3196{
3197 if (rhs_type->tt_type == VAR_VOID)
3198 {
3199 emsg(_(e_cannot_use_void_value));
3200 return FAIL;
3201 }
3202
3203 type_T *type;
3204
3205 // An empty list or dict has a &t_unknown member, for a variable that
3206 // implies &t_any.
3207 if (rhs_type == &t_list_empty)
3208 type = &t_list_any;
3209 else if (rhs_type == &t_dict_empty)
3210 type = &t_dict_any;
3211 else if (rhs_type == &t_unknown)
3212 type = &t_any;
3213 else
3214 {
3215 type = rhs_type;
3216 cac->cac_inferred_type = rhs_type;
3217 }
3218
3219 set_var_type(lhs->lhs_lvar, type, cctx);
3220
3221 return OK;
3222}
3223
3224/*
3225 * Returns TRUE if the "rhs_type" can be assigned to the "lhs" variable.
3226 * Used when compiling an assignment statement.
3227 */
3228 static int
3229compile_assign_valid_rhs_type(
3230 cctx_T *cctx,
3231 cac_T *cac,
3232 lhs_T *lhs,
3233 type_T *rhs_type)
3234{
3235 type_T *use_type = lhs->lhs_lvar->lv_type;
3236 where_T where = WHERE_INIT;
3237
3238 // Without operator check type here, otherwise below.
3239 // Use the line number of the assignment.
3240 SOURCING_LNUM = cac->cac_start_lnum;
3241 if (cac->cac_var_count > 0)
3242 {
3243 where.wt_index = cac->cac_var_idx + 1;
3244 where.wt_kind = WT_VARIABLE;
3245 }
3246
3247 // If assigning to a list or dict member, use the member type.
3248 // Not for "list[:] =".
3249 if (lhs->lhs_has_index &&
3250 !has_list_index(cac->cac_var_start + lhs->lhs_varlen, cctx))
3251 use_type = lhs->lhs_member_type;
3252
3253 if (need_type_where(rhs_type, use_type, FALSE, -1, where, cctx, FALSE,
3254 cac->cac_is_const) == FAIL)
3255 return FALSE;
3256
3257 return TRUE;
3258}
3259
3260/*
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003261 * Compare the LHS type with the RHS type in an assignment.
3262 */
3263 static int
3264compile_assign_check_type(cctx_T *cctx, cac_T *cac)
3265{
3266 lhs_T *lhs = &cac->cac_lhs;
3267 type_T *rhs_type;
3268
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003269 rhs_type = cctx->ctx_type_stack.ga_len == 0
3270 ? &t_void
3271 : get_type_on_stack(cctx, 0);
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003272 cac->cac_rhs_type = rhs_type;
3273
3274 if (check_type_is_value(rhs_type) == FAIL)
3275 return FAIL;
3276
3277 if (lhs->lhs_lvar != NULL && (cac->cac_is_decl || !lhs->lhs_has_type))
3278 {
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003279 if (rhs_type->tt_type == VAR_FUNC
3280 || rhs_type->tt_type == VAR_PARTIAL)
3281 {
3282 // Make sure the variable name can be used as a funcref
3283 if (!lhs->lhs_has_index
3284 && var_wrong_func_name(lhs->lhs_name, TRUE))
3285 return FAIL;
3286 }
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003287
3288 if (lhs->lhs_new_local && !lhs->lhs_has_type)
3289 {
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003290 // The LHS variable doesn't have a type. Set it to the RHS type.
3291 if (compile_assign_set_lhs_type_from_rhs(cctx, cac, lhs, rhs_type)
3292 == FAIL)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003293 return FAIL;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003294 }
3295 else if (*cac->cac_op == '=')
3296 {
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003297 if (!compile_assign_valid_rhs_type(cctx, cac, lhs, rhs_type))
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003298 return FAIL;
3299 }
3300 }
3301 else
3302 {
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003303 // Assigning to a register using @r = "abc"
3304
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003305 type_T *lhs_type = lhs->lhs_member_type;
3306
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003307 // Special case: assigning to @# can use a number or a string.
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003308 // Also: can assign a number to a float.
3309 if ((lhs_type == &t_number_or_string || lhs_type == &t_float)
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003310 && rhs_type->tt_type == VAR_NUMBER)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003311 lhs_type = &t_number;
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003312
3313 if (*cac->cac_nextc != '=')
3314 {
3315 if (need_type(rhs_type, lhs_type, FALSE, -1, 0, cctx, FALSE,
3316 FALSE) == FAIL)
3317 return FAIL;
3318 }
3319 }
3320
3321 return OK;
3322}
3323
3324/*
3325 * Compile the RHS expression in an assignment statement and generate the
3326 * instructions.
3327 */
3328 static int
3329compile_assign_rhs_expr(cctx_T *cctx, cac_T *cac)
3330{
3331 cac->cac_is_const = FALSE;
3332
3333 // for "+=", "*=", "..=" etc. first load the current value
3334 if (*cac->cac_op != '='
3335 && compile_load_lhs_with_index(&cac->cac_lhs, cac->cac_var_start,
3336 cctx) == FAIL)
3337 return FAIL;
3338
3339 // For "var = expr" evaluate the expression.
3340 if (cac->cac_var_count == 0)
3341 {
3342 int ret;
3343
3344 // Compile the expression.
3345 cac->cac_instr_count = cac->cac_instr->ga_len;
3346 ret = compile_assign_single_eval_expr(cctx, cac);
3347 if (ret == FAIL)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003348 return FAIL;
3349 }
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003350 else if (cac->cac_semicolon && cac->cac_var_idx == cac->cac_var_count - 1)
3351 {
3352 // For "[var; var] = expr" get the rest of the list
3353 cac->cac_did_generate_slice = TRUE;
3354 if (generate_SLICE(cctx, cac->cac_var_count - 1) == FAIL)
3355 return FAIL;
3356 }
3357 else
3358 {
3359 // For "[var, var] = expr" get the "var_idx" item from the
3360 // list.
3361 int with_op = *cac->cac_op != '=';
3362 if (generate_GETITEM(cctx, cac->cac_var_idx, with_op) == FAIL)
3363 return FAIL;
3364 }
3365
3366 if (compile_assign_check_type(cctx, cac) == FAIL)
3367 return FAIL;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003368
3369 return OK;
3370}
3371
3372/*
3373 * Compile the RHS expression in an assignment
3374 */
3375 static int
3376compile_assign_rhs(cctx_T *cctx, cac_T *cac)
3377{
3378 lhs_T *lhs = &cac->cac_lhs;
3379
3380 if (cctx->ctx_skip == SKIP_YES)
3381 {
3382 if (cac->cac_oplen > 0 && cac->cac_var_count == 0)
3383 {
3384 // skip over the "=" and the expression
3385 cac->cac_nextc = skipwhite(cac->cac_op + cac->cac_oplen);
3386 (void)compile_expr0(&cac->cac_nextc, cctx);
3387 }
3388 return OK;
3389 }
3390
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003391 // If RHS is specified, then generate instructions for RHS expression
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003392 if (cac->cac_oplen > 0)
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003393 return compile_assign_rhs_expr(cctx, cac);
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003394
3395 if (cac->cac_cmdidx == CMD_final)
3396 {
3397 emsg(_(e_final_requires_a_value));
3398 return FAIL;
3399 }
3400
3401 if (cac->cac_cmdidx == CMD_const)
3402 {
3403 emsg(_(e_const_requires_a_value));
3404 return FAIL;
3405 }
3406
3407 if (!lhs->lhs_has_type || lhs->lhs_dest == dest_option
3408 || lhs->lhs_dest == dest_func_option)
3409 {
3410 emsg(_(e_type_or_initialization_required));
3411 return FAIL;
3412 }
3413
3414 // variables are always initialized
3415 if (GA_GROW_FAILS(cac->cac_instr, 1))
3416 return FAIL;
3417
3418 cac->cac_instr_count = cac->cac_instr->ga_len;
3419
3420 return push_default_value(cctx, lhs->lhs_member_type->tt_type,
3421 lhs->lhs_dest == dest_local,
3422 &cac->cac_skip_store);
3423}
3424
3425/*
3426 * Compile a compound op assignment statement (+=, -=, *=, %=, etc.)
3427 */
3428 static int
3429compile_assign_compound_op(cctx_T *cctx, cac_T *cac)
3430{
3431 lhs_T *lhs = &cac->cac_lhs;
3432 type_T *expected;
3433 type_T *stacktype = NULL;
3434
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01003435 if (cac->cac_lhs.lhs_type->tt_type == VAR_TUPLE)
3436 {
3437 // compound operators are not supported with a tuple
3438 char_u op[2];
3439
3440 op[0] = *cac->cac_op;
3441 op[1] = NUL;
3442 semsg(_(e_wrong_variable_type_for_str_equal), op);
3443 return FAIL;
3444 }
3445
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003446 if (*cac->cac_op == '.')
3447 {
Hirohito Higashi2ffd35f2025-07-08 21:47:01 +02003448 expected = lhs->lhs_member_type;
3449 stacktype = get_type_on_stack(cctx, 0);
3450 if (expected != &t_string
3451 && need_type(stacktype, expected, FALSE, -1, 0, cctx,
3452 FALSE, FALSE) == FAIL)
3453 return FAIL;
3454 else if (may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003455 return FAIL;
3456 }
3457 else
3458 {
3459 expected = lhs->lhs_member_type;
3460 stacktype = get_type_on_stack(cctx, 0);
3461 if (
3462 // If variable is float operation with number is OK.
3463 !(expected == &t_float && (stacktype == &t_number
3464 || stacktype == &t_number_bool))
3465 && need_type(stacktype, expected, TRUE, -1, 0, cctx,
3466 FALSE, FALSE) == FAIL)
3467 return FAIL;
3468 }
3469
3470 if (*cac->cac_op == '.')
3471 {
3472 if (generate_CONCAT(cctx, 2) == FAIL)
3473 return FAIL;
3474 }
3475 else if (*cac->cac_op == '+')
3476 {
3477 if (generate_add_instr(cctx,
3478 operator_type(lhs->lhs_member_type, stacktype),
3479 lhs->lhs_member_type, stacktype,
3480 EXPR_APPEND) == FAIL)
3481 return FAIL;
3482 }
3483 else if (generate_two_op(cctx, cac->cac_op) == FAIL)
3484 return FAIL;
3485
3486 return OK;
3487}
3488
3489/*
3490 * Generate the STORE and SETTYPE instructions for an assignment statement.
3491 */
3492 static int
3493compile_assign_generate_store(cctx_T *cctx, cac_T *cac)
3494{
3495 lhs_T *lhs = &cac->cac_lhs;
3496 int save_lnum;
3497
3498 // Use the line number of the assignment for store instruction.
3499 save_lnum = cctx->ctx_lnum;
3500 cctx->ctx_lnum = cac->cac_start_lnum - 1;
3501
3502 if (lhs->lhs_has_index)
3503 {
3504 // Use the info in "lhs" to store the value at the index in the
3505 // list, dict or object.
3506 if (compile_assign_unlet(cac->cac_var_start, &cac->cac_lhs,
3507 TRUE, cac->cac_rhs_type, cctx) == FAIL)
3508 {
3509 cctx->ctx_lnum = save_lnum;
3510 return FAIL;
3511 }
3512 }
3513 else
3514 {
3515 if (cac->cac_is_decl && cac->cac_cmdidx == CMD_const &&
3516 (lhs->lhs_dest == dest_script
3517 || lhs->lhs_dest == dest_script_v9
3518 || lhs->lhs_dest == dest_global
3519 || lhs->lhs_dest == dest_local))
3520 // ":const var": lock the value, but not referenced variables
3521 generate_LOCKCONST(cctx);
3522
3523 type_T *inferred_type = cac->cac_inferred_type;
3524
3525 if ((lhs->lhs_type->tt_type == VAR_DICT
3526 || lhs->lhs_type->tt_type == VAR_LIST)
3527 && lhs->lhs_type->tt_member != NULL
3528 && lhs->lhs_type->tt_member != &t_any
3529 && lhs->lhs_type->tt_member != &t_unknown)
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01003530 // Set the type in the list or dict, so that it can be
3531 // checked, also in legacy script.
3532 generate_SETTYPE(cctx, lhs->lhs_type);
3533 else if (lhs->lhs_type->tt_type == VAR_TUPLE
3534 && lhs->lhs_type->tt_argcount != 0)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003535 generate_SETTYPE(cctx, lhs->lhs_type);
3536 else if (inferred_type != NULL
3537 && (inferred_type->tt_type == VAR_DICT
3538 || inferred_type->tt_type == VAR_LIST)
3539 && inferred_type->tt_member != NULL
3540 && inferred_type->tt_member != &t_unknown
3541 && inferred_type->tt_member != &t_any)
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01003542 // Set the type in the list or dict, so that it can be
3543 // checked, also in legacy script.
3544 generate_SETTYPE(cctx, inferred_type);
3545 else if (inferred_type != NULL
3546 && inferred_type->tt_type == VAR_TUPLE
3547 && inferred_type->tt_argcount > 0)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003548 generate_SETTYPE(cctx, inferred_type);
3549
3550 if (!cac->cac_skip_store &&
3551 generate_store_lhs(cctx, &cac->cac_lhs,
3552 cac->cac_instr_count,
3553 cac->cac_is_decl) == FAIL)
3554 {
3555 cctx->ctx_lnum = save_lnum;
3556 return FAIL;
3557 }
3558 }
3559
3560 cctx->ctx_lnum = save_lnum;
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003561
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003562 return OK;
3563}
3564
3565/*
3566 * Process the variable(s) in an assignment statement
3567 */
3568 static int
3569compile_assign_process_variables(
3570 cctx_T *cctx,
3571 cac_T *cac,
3572 int cmdidx,
3573 int heredoc,
3574 int has_cmd,
3575 int has_argisset_prefix,
3576 int jump_instr_idx)
3577{
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003578 /*
3579 * Loop over variables in "[var, var] = expr".
3580 * For "name = expr" and "var name: type" this is done only once.
3581 */
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003582 for (cac->cac_var_idx = 0; cac->cac_var_idx == 0 ||
3583 cac->cac_var_idx < cac->cac_var_count; cac->cac_var_idx++)
3584 {
3585 if (cac->cac_var_start[0] == '_'
3586 && !eval_isnamec(cac->cac_var_start[1]))
3587 {
3588 // Ignore underscore in "[a, _, b] = list".
3589 if (cac->cac_var_count > 0)
3590 {
3591 cac->cac_var_start = skipwhite(cac->cac_var_start + 2);
3592 continue;
3593 }
3594 emsg(_(e_cannot_use_underscore_here));
3595 return FAIL;
3596 }
3597 vim_free(cac->cac_lhs.lhs_name);
3598
3599 /*
3600 * Figure out the LHS type and other properties.
3601 */
3602 if (compile_assign_lhs(cac->cac_var_start, &cac->cac_lhs, cmdidx,
3603 cac->cac_is_decl, heredoc, has_cmd,
3604 cac->cac_oplen, cctx) == FAIL)
3605 return FAIL;
3606
3607 // Compile the RHS expression
3608 if (heredoc)
3609 {
3610 SOURCING_LNUM = cac->cac_start_lnum;
3611 if (cac->cac_lhs.lhs_has_type
3612 && need_type(&t_list_string, cac->cac_lhs.lhs_type,
3613 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
3614 return FAIL;
3615 }
3616 else
3617 {
3618 if (compile_assign_rhs(cctx, cac) == FAIL)
3619 return FAIL;
3620 if (cac->cac_var_count == 0)
3621 cac->cac_var_end = cac->cac_nextc;
3622 }
3623
3624 // no need to parse more when skipping
3625 if (cctx->ctx_skip == SKIP_YES)
3626 break;
3627
3628 if (cac->cac_oplen > 0 && *cac->cac_op != '=')
3629 {
3630 if (compile_assign_compound_op(cctx, cac) == FAIL)
3631 return FAIL;
3632 }
3633
3634 // generate the store instructions
3635 if (compile_assign_generate_store(cctx, cac) == FAIL)
3636 return FAIL;
3637
3638 if (cac->cac_var_idx + 1 < cac->cac_var_count)
3639 cac->cac_var_start = skipwhite(cac->cac_lhs.lhs_end + 1);
3640
3641 if (has_argisset_prefix)
3642 {
3643 // set instruction index in JUMP_IF_ARG_SET to here
3644 isn_T *isn = ((isn_T *)cac->cac_instr->ga_data) + jump_instr_idx;
3645 isn->isn_arg.jumparg.jump_where = cac->cac_instr->ga_len;
3646 }
3647 }
3648
3649 return OK;
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003650}
3651
3652/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003653 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003654 * "let name"
3655 * "var name = expr"
3656 * "final name = expr"
3657 * "const name = expr"
3658 * "name = expr"
3659 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003660 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003661 * Return NULL for an error.
3662 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003663 */
3664 static char_u *
Bram Moolenaar65b0d162022-12-13 18:43:22 +00003665compile_assignment(
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003666 char_u *arg_start,
3667 exarg_T *eap,
3668 cmdidx_T cmdidx,
3669 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003670{
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003671 cac_T cac;
Bram Moolenaar65b0d162022-12-13 18:43:22 +00003672 char_u *arg = arg_start;
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003673 char_u *retstr = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003674 int heredoc = FALSE;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003675 int jump_instr_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003677 compile_assign_context_init(&cac, cctx, cmdidx, arg);
3678
3679 jump_instr_idx = cac.cac_instr->ga_len;
3680
3681 // process object variable initialization in a new() constructor method
3682 int has_argisset_prefix = STRNCMP(arg, "ifargisset ", 11) == 0;
3683 if (has_argisset_prefix &&
3684 compile_assign_obj_new_arg(&arg, cctx) == FAIL)
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003685 goto theend;
Bram Moolenaar65b0d162022-12-13 18:43:22 +00003686
Bram Moolenaare1d12112022-03-05 11:37:48 +00003687 // Skip over the "varname" or "[varname, varname]" to get to any "=".
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003688 cac.cac_nextc = skip_var_list(arg, TRUE, &cac.cac_var_count,
3689 &cac.cac_semicolon, TRUE);
3690 if (cac.cac_nextc == NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003691 return *arg == '[' ? arg : NULL;
3692
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003693 if (compile_assign_process_operator(eap, arg, &cac, &heredoc,
3694 &retstr) == FAIL)
3695 return retstr;
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003696
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003697 // Compute the start of the assignment
3698 cac.cac_var_start = compile_assign_compute_start(arg, cac.cac_var_count);
3699
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003700 // Compute the end of the assignment
3701 cac.cac_var_end = compile_assign_compute_end(eap, cctx, &cac, heredoc);
3702 if (cac.cac_var_end == NULL)
3703 return NULL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003704
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003705 int has_cmd = cac.cac_var_start > eap->cmd;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003706
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003707 /* process the variable(s) */
3708 if (compile_assign_process_variables(cctx, &cac, cmdidx, heredoc,
3709 has_cmd, has_argisset_prefix,
3710 jump_instr_idx) == FAIL)
3711 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003712
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02003713 // For "[var, var] = expr" drop the "expr" value.
3714 // Also for "[var, var; _] = expr".
Yegappan Lakshmanane2038412024-12-14 19:59:24 +01003715 if (cctx->ctx_skip != SKIP_YES && cac.cac_var_count > 0 &&
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003716 (!cac.cac_semicolon || !cac.cac_did_generate_slice))
Bram Moolenaar9af78762020-06-16 11:34:42 +02003717 {
Bram Moolenaarec792292020-12-13 21:26:56 +01003718 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02003719 goto theend;
3720 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003721
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003722 retstr = skipwhite(cac.cac_var_end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003723
3724theend:
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003725 vim_free(cac.cac_lhs.lhs_name);
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003726 return retstr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003727}
3728
3729/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01003730 * Check for an assignment at "eap->cmd", compile it if found.
3731 * Return NOTDONE if there is none, FAIL for failure, OK if done.
3732 */
3733 static int
3734may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
3735{
3736 char_u *pskip;
3737 char_u *p;
3738
3739 // Assuming the command starts with a variable or function name,
3740 // find what follows.
3741 // Skip over "var.member", "var[idx]" and the like.
3742 // Also "&opt = val", "$ENV = val" and "@r = val".
3743 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
3744 ? eap->cmd + 1 : eap->cmd;
3745 p = to_name_end(pskip, TRUE);
3746 if (p > eap->cmd && *p != NUL)
3747 {
3748 char_u *var_end;
3749 int oplen;
3750 int heredoc;
3751
3752 if (eap->cmd[0] == '@')
3753 var_end = eap->cmd + 2;
3754 else
3755 var_end = find_name_end(pskip, NULL, NULL,
3756 FNE_CHECK_START | FNE_INCL_BR);
3757 oplen = assignment_len(skipwhite(var_end), &heredoc);
3758 if (oplen > 0)
3759 {
3760 size_t len = p - eap->cmd;
3761
3762 // Recognize an assignment if we recognize the variable
3763 // name:
Bram Moolenaar17126b12021-01-07 22:03:02 +01003764 // "&opt = expr"
3765 // "$ENV = expr"
3766 // "@r = expr"
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003767 // "g:var = expr"
3768 // "g:[key] = expr"
3769 // "local = expr" where "local" is a local var.
3770 // "script = expr" where "script" is a script-local var.
3771 // "import = expr" where "import" is an imported var
Bram Moolenaar17126b12021-01-07 22:03:02 +01003772 if (*eap->cmd == '&'
3773 || *eap->cmd == '$'
3774 || *eap->cmd == '@'
3775 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003776 || STRNCMP(eap->cmd, "g:[", 3) == 0
Bram Moolenaare0890d62021-02-17 14:52:14 +01003777 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01003778 {
3779 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
3780 if (*line == NULL || *line == eap->cmd)
3781 return FAIL;
3782 return OK;
3783 }
3784 }
3785 }
3786
Bram Moolenaar65b0d162022-12-13 18:43:22 +00003787 // might be "[var, var] = expr" or "ifargisset this.member = expr"
3788 if (*eap->cmd == '[' || STRNCMP(eap->cmd, "ifargisset ", 11) == 0)
Bram Moolenaar17126b12021-01-07 22:03:02 +01003789 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01003790 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
3791 if (*line == NULL)
3792 return FAIL;
3793 if (*line != eap->cmd)
3794 return OK;
3795 }
3796 return NOTDONE;
3797}
3798
Bram Moolenaar9a015112021-12-31 14:06:45 +00003799/*
3800 * Check if arguments of "ufunc" shadow variables in "cctx".
3801 * Return OK or FAIL.
3802 */
3803 static int
3804check_args_shadowing(ufunc_T *ufunc, cctx_T *cctx)
3805{
3806 int i;
3807 char_u *arg;
3808 int r = OK;
3809
3810 // Make sure arguments are not found when compiling a second time.
3811 ufunc->uf_args_visible = 0;
3812
3813 // Check for arguments shadowing variables from the context.
3814 for (i = 0; i < ufunc->uf_args.ga_len; ++i)
3815 {
3816 arg = ((char_u **)(ufunc->uf_args.ga_data))[i];
Bram Moolenaardce24412022-02-08 20:35:30 +00003817 if (check_defined(arg, STRLEN(arg), cctx, NULL, TRUE) == FAIL)
Bram Moolenaar9a015112021-12-31 14:06:45 +00003818 {
3819 r = FAIL;
3820 break;
3821 }
3822 }
3823 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
3824 return r;
3825}
3826
Bram Moolenaarfcb86b02022-10-07 22:46:24 +01003827#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar139575d2022-03-15 19:29:30 +00003828/*
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003829 * Get a count before a command. Can only be a number.
3830 * Returns zero if there is no count.
3831 * Returns -1 if there is something wrong.
3832 */
3833 static long
3834get_cmd_count(char_u *line, exarg_T *eap)
3835{
3836 char_u *p;
3837
3838 // skip over colons and white space
3839 for (p = line; *p == ':' || VIM_ISWHITE(*p); ++p)
3840 ;
Keith Thompson184f71c2024-01-04 21:19:04 +01003841 if (!SAFE_isdigit(*p))
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003842 {
Bram Moolenaar2435adf2022-10-21 12:05:46 +01003843 // The command or modifiers must be following. Assume a lower case
3844 // character means there is a modifier.
3845 if (p < eap->cmd && !vim_islower(*p))
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003846 {
3847 emsg(_(e_invalid_range));
3848 return -1;
3849 }
3850 return 0;
3851 }
3852 return atol((char *)p);
3853}
Bram Moolenaarfcb86b02022-10-07 22:46:24 +01003854#endif
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003855
3856/*
Bram Moolenaar139575d2022-03-15 19:29:30 +00003857 * Get the compilation type that should be used for "ufunc".
3858 * Keep in sync with INSTRUCTIONS().
3859 */
3860 compiletype_T
3861get_compile_type(ufunc_T *ufunc)
3862{
3863 // Update uf_has_breakpoint if needed.
3864 update_has_breakpoint(ufunc);
3865
3866 if (debug_break_level > 0 || may_break_in_function(ufunc))
3867 return CT_DEBUG;
3868#ifdef FEAT_PROFILE
3869 if (do_profiling == PROF_YES)
3870 {
Ernie Rael21d32122023-09-02 15:09:18 +02003871 if (!ufunc->uf_profiling && has_profiling(FALSE, ufunc->uf_name, NULL,
3872 &ufunc->uf_hash))
Bram Moolenaar139575d2022-03-15 19:29:30 +00003873 func_do_profile(ufunc);
3874 if (ufunc->uf_profiling)
3875 return CT_PROFILE;
3876 }
3877#endif
3878 return CT_NONE;
3879}
3880
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01003881/*
3882 * Free the compiled instructions saved for a def function. This is used when
3883 * compiling a def function and the function was compiled before.
3884 * The index is reused.
3885 */
3886 static void
3887clear_def_function(ufunc_T *ufunc, compiletype_T compile_type)
3888{
3889 isn_T *instr_dest = NULL;
3890 dfunc_T *dfunc;
3891
3892 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
3893
3894 switch (compile_type)
3895 {
3896 case CT_PROFILE:
3897#ifdef FEAT_PROFILE
3898 instr_dest = dfunc->df_instr_prof; break;
3899#endif
3900 case CT_NONE: instr_dest = dfunc->df_instr; break;
3901 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
3902 }
3903
3904 if (instr_dest != NULL)
3905 // Was compiled in this mode before: Free old instructions.
3906 delete_def_function_contents(dfunc, FALSE);
3907
3908 ga_clear_strings(&dfunc->df_var_names);
3909 dfunc->df_defer_var_idx = 0;
3910}
Bram Moolenaar7b829262021-10-13 15:04:34 +01003911
3912/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02003913 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003914 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02003915 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02003916 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02003917add_def_function(ufunc_T *ufunc)
3918{
3919 dfunc_T *dfunc;
3920
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003921 if (def_functions.ga_len == 0)
3922 {
3923 // The first position is not used, so that a zero uf_dfunc_idx means it
3924 // wasn't set.
Bram Moolenaar35578162021-08-02 19:10:38 +02003925 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003926 return FAIL;
3927 ++def_functions.ga_len;
3928 }
3929
Bram Moolenaar09689a02020-05-09 22:50:08 +02003930 // Add the function to "def_functions".
Bram Moolenaar35578162021-08-02 19:10:38 +02003931 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar09689a02020-05-09 22:50:08 +02003932 return FAIL;
3933 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
3934 CLEAR_POINTER(dfunc);
3935 dfunc->df_idx = def_functions.ga_len;
3936 ufunc->uf_dfunc_idx = dfunc->df_idx;
3937 dfunc->df_ufunc = ufunc;
John Marriottb32800f2025-02-01 15:25:34 +01003938 dfunc->df_name = vim_strnsave(ufunc->uf_name, ufunc->uf_namelen);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02003939 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003940 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02003941 ++def_functions.ga_len;
3942 return OK;
3943}
3944
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01003945 static int
3946compile_dfunc_ufunc_init(
3947 ufunc_T *ufunc,
3948 cctx_T *outer_cctx,
3949 compiletype_T compile_type,
3950 int *new_def_function)
3951{
3952 // When using a function that was compiled before: Free old instructions.
3953 // The index is reused. Otherwise add a new entry in "def_functions".
3954 if (ufunc->uf_dfunc_idx > 0)
3955 clear_def_function(ufunc, compile_type);
3956 else
3957 {
3958 if (add_def_function(ufunc) == FAIL)
3959 return FAIL;
3960
3961 *new_def_function = TRUE;
3962 }
3963
3964 if ((ufunc->uf_flags & FC_CLOSURE) && outer_cctx == NULL)
3965 {
3966 semsg(_(e_compiling_closure_without_context_str),
3967 printable_func_name(ufunc));
3968 return FAIL;
3969 }
3970
3971 ufunc->uf_def_status = UF_COMPILING;
3972
3973 return OK;
3974}
3975
3976/*
3977 * Initialize the compilation context for compiling a def function.
3978 */
3979 static void
3980compile_dfunc_cctx_init(
3981 cctx_T *cctx,
3982 cctx_T *outer_cctx,
3983 ufunc_T *ufunc,
3984 compiletype_T compile_type)
3985{
3986 CLEAR_FIELD(*cctx);
3987
3988 cctx->ctx_compile_type = compile_type;
3989 cctx->ctx_ufunc = ufunc;
3990 cctx->ctx_lnum = -1;
3991 cctx->ctx_outer = outer_cctx;
3992 ga_init2(&cctx->ctx_locals, sizeof(lvar_T), 10);
3993 // Each entry on the type stack consists of two type pointers.
3994 ga_init2(&cctx->ctx_type_stack, sizeof(type2_T), 50);
3995 cctx->ctx_type_list = &ufunc->uf_type_list;
3996 ga_init2(&cctx->ctx_instr, sizeof(isn_T), 50);
3997}
3998
Bram Moolenaar09689a02020-05-09 22:50:08 +02003999/*
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004000 * For an object constructor, generate instruction to setup "this" (the first
4001 * local variable) and to initialize the object variables.
4002 */
4003 static int
4004obj_constructor_prologue(ufunc_T *ufunc, cctx_T *cctx)
4005{
4006 generate_CONSTRUCT(cctx, ufunc->uf_class);
4007
4008 for (int i = 0; i < ufunc->uf_class->class_obj_member_count; ++i)
4009 {
4010 ocmember_T *m = &ufunc->uf_class->class_obj_members[i];
4011
4012 if (i < 2 && IS_ENUM(ufunc->uf_class))
4013 // The first two object variables in an enum are the name
4014 // and the ordinal. These are set by the ISN_CONSTRUCT
4015 // instruction. So don't generate instructions to set
4016 // these variables.
4017 continue;
4018
4019 if (m->ocm_init != NULL)
4020 {
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +01004021 char_u *expr = m->ocm_init;
4022 sctx_T save_current_sctx;
4023 int change_sctx = FALSE;
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004024
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +01004025 // If the member variable initialization script context is
4026 // different from the current script context, then change it.
4027 if (current_sctx.sc_sid != m->ocm_init_sctx.sc_sid)
4028 change_sctx = TRUE;
4029
4030 if (change_sctx)
4031 {
4032 // generate an instruction to change the script context to the
4033 // member variable initialization script context.
4034 save_current_sctx = current_sctx;
4035 current_sctx = m->ocm_init_sctx;
4036 generate_SCRIPTCTX_SET(cctx, current_sctx);
4037 }
4038
4039 int r = compile_expr0(&expr, cctx);
4040
4041 if (change_sctx)
4042 {
4043 // restore the previous script context
4044 current_sctx = save_current_sctx;
4045 generate_SCRIPTCTX_SET(cctx, current_sctx);
4046 }
4047
4048 if (r == FAIL)
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004049 return FAIL;
4050
4051 if (!ends_excmd2(m->ocm_init, expr))
4052 {
4053 semsg(_(e_trailing_characters_str), expr);
4054 return FAIL;
4055 }
4056
4057 type_T *type = get_type_on_stack(cctx, 0);
4058 if (m->ocm_type->tt_type == VAR_ANY
4059 && !(m->ocm_flags & OCMFLAG_HAS_TYPE)
4060 && type->tt_type != VAR_SPECIAL)
4061 {
4062 // If the member variable type is not yet set, then use
4063 // the initialization expression type.
4064 m->ocm_type = type;
4065 }
LemonBoyf4af3312024-07-04 13:43:12 +02004066 else
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004067 {
4068 // The type of the member initialization expression is
4069 // determined at run time. Add a runtime type check.
4070 where_T where = WHERE_INIT;
4071 where.wt_kind = WT_MEMBER;
4072 where.wt_func_name = (char *)m->ocm_name;
4073 if (need_type_where(type, m->ocm_type, FALSE, -1,
4074 where, cctx, FALSE, FALSE) == FAIL)
4075 return FAIL;
4076 }
4077 }
4078 else
4079 push_default_value(cctx, m->ocm_type->tt_type, FALSE, NULL);
4080
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01004081 if (((m->ocm_type->tt_type == VAR_DICT
4082 || m->ocm_type->tt_type == VAR_LIST)
4083 && m->ocm_type->tt_member != NULL
4084 && m->ocm_type->tt_member != &t_any
4085 && m->ocm_type->tt_member != &t_unknown)
4086 || (m->ocm_type->tt_type == VAR_TUPLE
4087 && m->ocm_type->tt_argcount > 0))
4088 // Set the type in the list, tuple or dict, so that it can be
4089 // checked, also in legacy script.
LemonBoyf4af3312024-07-04 13:43:12 +02004090 generate_SETTYPE(cctx, m->ocm_type);
4091
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004092 generate_STORE_THIS(cctx, i);
4093 }
4094
4095 return OK;
4096}
4097
4098/*
4099 * For an object method and an constructor, generate instruction to setup
4100 * "this" (the first local variable). For a constructor, generate instructions
4101 * to initialize the object variables.
4102 */
4103 static int
4104obj_method_prologue(ufunc_T *ufunc, cctx_T *cctx)
4105{
4106 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
4107
4108 if (GA_GROW_FAILS(&dfunc->df_var_names, 1))
4109 return FAIL;
4110
4111 ((char_u **)dfunc->df_var_names.ga_data)[0] =
4112 vim_strsave((char_u *)"this");
4113 ++dfunc->df_var_names.ga_len;
4114
4115 // In the constructor allocate memory for the object and initialize the
4116 // object members.
4117 if (IS_CONSTRUCTOR_METHOD(ufunc))
4118 return obj_constructor_prologue(ufunc, cctx);
4119
4120 return OK;
4121}
4122
4123/*
4124 * Produce instructions for the default values of optional arguments.
4125 */
4126 static int
4127compile_def_function_default_args(
4128 ufunc_T *ufunc,
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004129 garray_T *instr,
4130 cctx_T *cctx)
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004131{
4132 int count = ufunc->uf_def_args.ga_len;
4133 int first_def_arg = ufunc->uf_args.ga_len - count;
4134 int i;
4135 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
4136 int did_set_arg_type = FALSE;
4137
4138 // Produce instructions for the default values of optional arguments.
4139 SOURCING_LNUM = 0; // line number unknown
4140 for (i = 0; i < count; ++i)
4141 {
4142 char_u *arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
4143 if (STRCMP(arg, "v:none") == 0)
4144 // "arg = v:none" means the argument is optional without
4145 // setting a value when the argument is missing.
4146 continue;
4147
4148 type_T *val_type;
4149 int arg_idx = first_def_arg + i;
4150 where_T where = WHERE_INIT;
4151 int jump_instr_idx = instr->ga_len;
4152 isn_T *isn;
4153
4154 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
4155 if (generate_JUMP_IF_ARG(cctx, ISN_JUMP_IF_ARG_SET,
4156 i - count - off) == FAIL)
4157 return FAIL;
4158
4159 // Make sure later arguments are not found.
4160 ufunc->uf_args_visible = arg_idx;
4161
4162 int r = compile_expr0(&arg, cctx);
4163 if (r == FAIL)
4164 return FAIL;
4165
4166 // If no type specified use the type of the default value.
4167 // Otherwise check that the default value type matches the
4168 // specified type.
4169 val_type = get_type_on_stack(cctx, 0);
4170 where.wt_index = arg_idx + 1;
4171 where.wt_kind = WT_ARGUMENT;
4172 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
4173 {
4174 did_set_arg_type = TRUE;
4175 ufunc->uf_arg_types[arg_idx] = val_type;
4176 }
4177 else if (need_type_where(val_type, ufunc->uf_arg_types[arg_idx],
4178 FALSE, -1, where, cctx, FALSE, FALSE) == FAIL)
4179 return FAIL;
4180
4181 if (generate_STORE(cctx, ISN_STORE, i - count - off, NULL) == FAIL)
4182 return FAIL;
4183
4184 // set instruction index in JUMP_IF_ARG_SET to here
4185 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
4186 isn->isn_arg.jumparg.jump_where = instr->ga_len;
4187 }
4188
4189 if (did_set_arg_type)
4190 set_function_type(ufunc);
4191
4192 return OK;
4193}
4194
4195/*
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004196 * Compile def function body. Loop over all the lines in the function and
4197 * generate instructions.
4198 */
4199 static int
4200compile_def_function_body(
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004201 int last_func_lnum,
4202 int check_return_type,
4203 garray_T *lines_to_free,
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004204 char **errormsg,
4205 cctx_T *cctx)
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004206{
4207 char_u *line = NULL;
4208 char_u *p;
4209 int did_emsg_before = did_emsg;
4210#ifdef FEAT_PROFILE
4211 int prof_lnum = -1;
4212#endif
4213 int debug_lnum = -1;
4214
4215 for (;;)
4216 {
4217 exarg_T ea;
4218 int starts_with_colon = FALSE;
4219 char_u *cmd;
4220 cmdmod_T local_cmdmod;
4221
4222 // Bail out on the first error to avoid a flood of errors and report
4223 // the right line number when inside try/catch.
4224 if (did_emsg_before != did_emsg)
4225 return FAIL;
4226
4227 if (line != NULL && *line == '|')
4228 // the line continues after a '|'
4229 ++line;
4230 else if (line != NULL && *skipwhite(line) != NUL
4231 && !(*line == '#' && (line == cctx->ctx_line_start
4232 || VIM_ISWHITE(line[-1]))))
4233 {
4234 semsg(_(e_trailing_characters_str), line);
4235 return FAIL;
4236 }
4237 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
4238 return FAIL;
4239 else
4240 {
4241 line = next_line_from_context(cctx, FALSE);
4242 if (cctx->ctx_lnum >= last_func_lnum)
4243 {
4244 // beyond the last line
4245#ifdef FEAT_PROFILE
4246 if (cctx->ctx_skip != SKIP_YES)
4247 may_generate_prof_end(cctx, prof_lnum);
4248#endif
4249 break;
4250 }
4251 // Make a copy, splitting off nextcmd and removing trailing spaces
4252 // may change it.
4253 if (line != NULL)
4254 {
4255 line = vim_strsave(line);
4256 if (ga_add_string(lines_to_free, line) == FAIL)
4257 return FAIL;
4258 }
4259 }
4260
4261 CLEAR_FIELD(ea);
4262 ea.cmdlinep = &line;
4263 ea.cmd = skipwhite(line);
4264 ea.skip = cctx->ctx_skip == SKIP_YES;
4265
4266 if (*ea.cmd == '#')
4267 {
4268 // "#" starts a comment, but "#{" is an error
4269 if (vim9_bad_comment(ea.cmd))
4270 return FAIL;
4271 line = (char_u *)"";
4272 continue;
4273 }
4274
4275#ifdef FEAT_PROFILE
4276 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_lnum != prof_lnum
4277 && cctx->ctx_skip != SKIP_YES)
4278 {
4279 may_generate_prof_end(cctx, prof_lnum);
4280
4281 prof_lnum = cctx->ctx_lnum;
4282 generate_instr(cctx, ISN_PROF_START);
4283 }
4284#endif
4285 if (cctx->ctx_compile_type == CT_DEBUG && cctx->ctx_lnum != debug_lnum
4286 && cctx->ctx_skip != SKIP_YES)
4287 {
4288 debug_lnum = cctx->ctx_lnum;
4289 generate_instr_debug(cctx);
4290 }
4291 cctx->ctx_prev_lnum = cctx->ctx_lnum + 1;
4292
4293 // Some things can be recognized by the first character.
4294 switch (*ea.cmd)
4295 {
4296 case '}':
4297 {
4298 // "}" ends a block scope
4299 scopetype_T stype = cctx->ctx_scope == NULL
4300 ? NO_SCOPE : cctx->ctx_scope->se_type;
4301
4302 if (stype == BLOCK_SCOPE)
4303 {
4304 compile_endblock(cctx);
4305 line = ea.cmd;
4306 }
4307 else
4308 {
4309 emsg(_(e_using_rcurly_outside_if_block_scope));
4310 return FAIL;
4311 }
4312 if (line != NULL)
4313 line = skipwhite(ea.cmd + 1);
4314 continue;
4315 }
4316
4317 case '{':
4318 // "{" starts a block scope
4319 // "{'a': 1}->func() is something else
4320 if (ends_excmd(*skipwhite(ea.cmd + 1)))
4321 {
4322 line = compile_block(ea.cmd, cctx);
4323 continue;
4324 }
4325 break;
4326 }
4327
4328 /*
4329 * COMMAND MODIFIERS
4330 */
4331 cctx->ctx_has_cmdmod = FALSE;
4332 if (parse_command_modifiers(&ea, errormsg, &local_cmdmod, FALSE)
4333 == FAIL)
4334 return FAIL;
4335 generate_cmdmods(cctx, &local_cmdmod);
4336 undo_cmdmod(&local_cmdmod);
4337
4338 // Check if there was a colon after the last command modifier or before
4339 // the current position.
4340 for (p = ea.cmd; p >= line; --p)
4341 {
4342 if (*p == ':')
4343 starts_with_colon = TRUE;
4344 if (p < ea.cmd && !VIM_ISWHITE(*p))
4345 break;
4346 }
4347
4348 // Skip ":call" to get to the function name, unless using :legacy
4349 p = ea.cmd;
4350 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
4351 {
4352 if (checkforcmd(&ea.cmd, "call", 3))
4353 {
4354 if (*ea.cmd == '(')
4355 // not for "call()"
4356 ea.cmd = p;
4357 else
4358 ea.cmd = skipwhite(ea.cmd);
4359 }
4360
4361 if (!starts_with_colon)
4362 {
4363 int assign;
4364
4365 // Check for assignment after command modifiers.
4366 assign = may_compile_assignment(&ea, &line, cctx);
4367 if (assign == OK)
4368 goto nextline;
4369 if (assign == FAIL)
4370 return FAIL;
4371 }
4372 }
4373
4374 /*
4375 * COMMAND after range
4376 * 'text'->func() should not be confused with 'a mark
4377 * 0z1234->func() should not be confused with a zero line number
4378 * "++nr" and "--nr" are eval commands
4379 * in "$ENV->func()" the "$" is not a range
4380 * "123->func()" is a method call
4381 */
4382 cmd = ea.cmd;
4383 if ((*cmd != '$' || starts_with_colon)
4384 && (starts_with_colon
4385 || !(*cmd == '\''
4386 || (cmd[0] == '0' && cmd[1] == 'z')
4387 || (cmd[0] != NUL && cmd[0] == cmd[1]
4388 && (*cmd == '+' || *cmd == '-'))
4389 || number_method(cmd))))
4390 {
4391 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
4392 if (ea.cmd > cmd)
4393 {
4394 if (!starts_with_colon
4395 && !(local_cmdmod.cmod_flags & CMOD_LEGACY))
4396 {
4397 semsg(_(e_colon_required_before_range_str), cmd);
4398 return FAIL;
4399 }
4400 ea.addr_count = 1;
4401 if (ends_excmd2(line, ea.cmd))
4402 {
4403 // A range without a command: jump to the line.
4404 generate_EXEC(cctx, ISN_EXECRANGE,
4405 vim_strnsave(cmd, ea.cmd - cmd));
4406 line = ea.cmd;
4407 goto nextline;
4408 }
4409 }
4410 }
4411 p = find_ex_command(&ea, NULL,
4412 starts_with_colon || (local_cmdmod.cmod_flags & CMOD_LEGACY)
4413 ? NULL : item_exists, cctx);
4414
4415 if (p == NULL)
4416 {
4417 if (cctx->ctx_skip != SKIP_YES)
4418 semsg(_(e_ambiguous_use_of_user_defined_command_str), ea.cmd);
4419 return FAIL;
4420 }
4421
4422 // When using ":legacy cmd" always use compile_exec().
4423 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
4424 {
4425 char_u *start = ea.cmd;
4426
4427 switch (ea.cmdidx)
4428 {
4429 case CMD_if:
4430 case CMD_elseif:
4431 case CMD_else:
4432 case CMD_endif:
4433 case CMD_for:
4434 case CMD_endfor:
4435 case CMD_continue:
4436 case CMD_break:
4437 case CMD_while:
4438 case CMD_endwhile:
4439 case CMD_try:
4440 case CMD_catch:
4441 case CMD_finally:
4442 case CMD_endtry:
4443 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
4444 return FAIL;
4445 default: break;
4446 }
4447
4448 // ":legacy return expr" needs to be handled differently.
4449 if (checkforcmd(&start, "return", 4))
4450 ea.cmdidx = CMD_return;
4451 else
4452 ea.cmdidx = CMD_legacy;
4453 }
4454
4455 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
4456 {
4457 // "eval" is used for "val->func()" and "var" for "var = val", then
4458 // "p" is equal to "ea.cmd" for a valid command.
4459 if (ea.cmdidx == CMD_eval || ea.cmdidx == CMD_var)
4460 ;
4461 else if (cctx->ctx_skip == SKIP_YES)
4462 {
4463 line += STRLEN(line);
4464 goto nextline;
4465 }
4466 else
4467 {
4468 semsg(_(e_command_not_recognized_str), ea.cmd);
4469 return FAIL;
4470 }
4471 }
4472
4473 if ((cctx->ctx_had_return || cctx->ctx_had_throw)
4474 && ea.cmdidx != CMD_elseif
4475 && ea.cmdidx != CMD_else
4476 && ea.cmdidx != CMD_endif
4477 && ea.cmdidx != CMD_endfor
4478 && ea.cmdidx != CMD_endwhile
4479 && ea.cmdidx != CMD_catch
4480 && ea.cmdidx != CMD_finally
4481 && ea.cmdidx != CMD_endtry
4482 && !ignore_unreachable_code_for_testing)
4483 {
4484 semsg(_(e_unreachable_code_after_str),
4485 cctx->ctx_had_return ? "return" : "throw");
4486 return FAIL;
4487 }
Yegappan Lakshmananab9a8942024-12-30 09:56:34 +01004488
4489 // When processing the end of an if-else block, don't clear the
4490 // "ctx_had_throw" flag. If an if-else block ends in a "throw"
4491 // statement, then it is considered to end in a "return" statement.
4492 // The "ctx_had_throw" is cleared immediately after processing the
4493 // if-else block ending statement.
4494 // Otherwise, clear the "had_throw" flag.
4495 if (ea.cmdidx != CMD_else && ea.cmdidx != CMD_elseif
4496 && ea.cmdidx != CMD_endif)
4497 cctx->ctx_had_throw = FALSE;
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004498
4499 p = skipwhite(p);
4500 if (ea.cmdidx != CMD_SIZE
4501 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
4502 {
4503 if (ea.cmdidx >= 0)
4504 ea.argt = excmd_get_argt(ea.cmdidx);
4505 if ((ea.argt & EX_BANG) && *p == '!')
4506 {
4507 ea.forceit = TRUE;
4508 p = skipwhite(p + 1);
4509 }
4510 if ((ea.argt & EX_RANGE) == 0 && ea.addr_count > 0)
4511 {
4512 emsg(_(e_no_range_allowed));
4513 return FAIL;
4514 }
4515 }
4516
4517 switch (ea.cmdidx)
4518 {
4519 case CMD_def:
4520 case CMD_function:
4521 ea.arg = p;
4522 line = compile_nested_function(&ea, cctx, lines_to_free);
4523 break;
4524
4525 case CMD_return:
4526 line = compile_return(p, check_return_type,
4527 local_cmdmod.cmod_flags & CMOD_LEGACY, cctx);
4528 cctx->ctx_had_return = TRUE;
4529 break;
4530
4531 case CMD_let:
4532 emsg(_(e_cannot_use_let_in_vim9_script));
4533 break;
4534 case CMD_var:
4535 case CMD_final:
4536 case CMD_const:
4537 case CMD_increment:
4538 case CMD_decrement:
4539 line = compile_assignment(p, &ea, ea.cmdidx, cctx);
4540 if (line == p)
4541 {
4542 emsg(_(e_invalid_assignment));
4543 line = NULL;
4544 }
4545 break;
4546
4547 case CMD_unlet:
4548 case CMD_unlockvar:
4549 case CMD_lockvar:
4550 line = compile_unletlock(p, &ea, cctx);
4551 break;
4552
4553 case CMD_import:
4554 emsg(_(e_import_can_only_be_used_in_script));
4555 line = NULL;
4556 break;
4557
4558 case CMD_if:
4559 line = compile_if(p, cctx);
4560 break;
4561 case CMD_elseif:
4562 line = compile_elseif(p, cctx);
4563 cctx->ctx_had_return = FALSE;
Yegappan Lakshmananab9a8942024-12-30 09:56:34 +01004564 cctx->ctx_had_throw = FALSE;
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004565 break;
4566 case CMD_else:
4567 line = compile_else(p, cctx);
4568 cctx->ctx_had_return = FALSE;
Yegappan Lakshmananab9a8942024-12-30 09:56:34 +01004569 cctx->ctx_had_throw = FALSE;
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004570 break;
4571 case CMD_endif:
4572 line = compile_endif(p, cctx);
Yegappan Lakshmananab9a8942024-12-30 09:56:34 +01004573 cctx->ctx_had_throw = FALSE;
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004574 break;
4575
4576 case CMD_while:
4577 line = compile_while(p, cctx);
4578 break;
4579 case CMD_endwhile:
4580 line = compile_endwhile(p, cctx);
4581 cctx->ctx_had_return = FALSE;
4582 break;
4583
4584 case CMD_for:
4585 line = compile_for(p, cctx);
4586 break;
4587 case CMD_endfor:
4588 line = compile_endfor(p, cctx);
4589 cctx->ctx_had_return = FALSE;
4590 break;
4591 case CMD_continue:
4592 line = compile_continue(p, cctx);
4593 break;
4594 case CMD_break:
4595 line = compile_break(p, cctx);
4596 break;
4597
4598 case CMD_try:
4599 line = compile_try(p, cctx);
4600 break;
4601 case CMD_catch:
4602 line = compile_catch(p, cctx);
4603 cctx->ctx_had_return = FALSE;
4604 break;
4605 case CMD_finally:
4606 line = compile_finally(p, cctx);
4607 cctx->ctx_had_return = FALSE;
4608 break;
4609 case CMD_endtry:
4610 line = compile_endtry(p, cctx);
4611 break;
4612 case CMD_throw:
4613 line = compile_throw(p, cctx);
4614 cctx->ctx_had_throw = TRUE;
4615 break;
4616
4617 case CMD_eval:
4618 line = compile_eval(p, cctx);
4619 break;
4620
4621 case CMD_defer:
4622 line = compile_defer(p, cctx);
4623 break;
4624
4625#ifdef HAS_MESSAGE_WINDOW
4626 case CMD_echowindow:
4627 {
4628 long cmd_count = get_cmd_count(line, &ea);
4629 if (cmd_count < 0)
4630 line = NULL;
4631 else
4632 line = compile_mult_expr(p, ea.cmdidx,
4633 cmd_count, cctx);
4634 }
4635 break;
4636#endif
4637 case CMD_echo:
4638 case CMD_echon:
4639 case CMD_echoconsole:
4640 case CMD_echoerr:
4641 case CMD_echomsg:
4642 case CMD_execute:
4643 line = compile_mult_expr(p, ea.cmdidx, 0, cctx);
4644 break;
4645
4646 case CMD_put:
4647 ea.cmd = cmd;
64-bitmane08f10a2025-03-18 22:14:34 +01004648 line = compile_put(p, &ea, cctx, FALSE);
4649 break;
4650
4651 case CMD_iput:
4652 ea.cmd = cmd;
4653 line = compile_put(p, &ea, cctx, TRUE);
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004654 break;
4655
4656 case CMD_substitute:
4657 if (check_global_and_subst(ea.cmd, p) == FAIL)
4658 return FAIL;
4659 if (cctx->ctx_skip == SKIP_YES)
4660 line = (char_u *)"";
4661 else
4662 {
4663 ea.arg = p;
4664 line = compile_substitute(line, &ea, cctx);
4665 }
4666 break;
4667
4668 case CMD_redir:
4669 ea.arg = p;
4670 line = compile_redir(line, &ea, cctx);
4671 break;
4672
4673 case CMD_cexpr:
4674 case CMD_lexpr:
4675 case CMD_caddexpr:
4676 case CMD_laddexpr:
4677 case CMD_cgetexpr:
4678 case CMD_lgetexpr:
4679#ifdef FEAT_QUICKFIX
4680 ea.arg = p;
4681 line = compile_cexpr(line, &ea, cctx);
4682#else
4683 ex_ni(&ea);
4684 line = NULL;
4685#endif
4686 break;
4687
4688 case CMD_append:
4689 case CMD_change:
4690 case CMD_insert:
4691 case CMD_k:
4692 case CMD_t:
4693 case CMD_xit:
4694 not_in_vim9(&ea);
4695 return FAIL;
4696
4697 case CMD_SIZE:
4698 if (cctx->ctx_skip != SKIP_YES)
4699 {
4700 semsg(_(e_invalid_command_str), ea.cmd);
4701 return FAIL;
4702 }
4703 // We don't check for a next command here.
4704 line = (char_u *)"";
4705 break;
4706
4707 case CMD_lua:
4708 case CMD_mzscheme:
4709 case CMD_perl:
4710 case CMD_py3:
4711 case CMD_python3:
4712 case CMD_python:
4713 case CMD_pythonx:
4714 case CMD_ruby:
4715 case CMD_tcl:
4716 ea.arg = p;
4717 if (vim_strchr(line, '\n') == NULL)
4718 line = compile_exec(line, &ea, cctx);
4719 else
4720 // heredoc lines have been concatenated with NL
4721 // characters in get_function_body()
4722 line = compile_script(line, cctx);
4723 break;
4724
4725 case CMD_vim9script:
4726 if (cctx->ctx_skip != SKIP_YES)
4727 {
4728 emsg(_(e_vim9script_can_only_be_used_in_script));
4729 return FAIL;
4730 }
4731 line = (char_u *)"";
4732 break;
4733
4734 case CMD_class:
4735 emsg(_(e_class_can_only_be_used_in_script));
4736 return FAIL;
4737
4738 case CMD_type:
4739 emsg(_(e_type_can_only_be_used_in_script));
4740 return FAIL;
4741
4742 case CMD_global:
4743 if (check_global_and_subst(ea.cmd, p) == FAIL)
4744 return FAIL;
4745 // FALLTHROUGH
4746 default:
4747 // Not recognized, execute with do_cmdline_cmd().
4748 ea.arg = p;
4749 line = compile_exec(line, &ea, cctx);
4750 break;
4751 }
4752nextline:
4753 if (line == NULL)
4754 return FAIL;
4755 line = skipwhite(line);
4756
4757 // Undo any command modifiers.
4758 generate_undo_cmdmods(cctx);
4759
4760 if (cctx->ctx_type_stack.ga_len < 0)
4761 {
4762 iemsg("Type stack underflow");
4763 return FAIL;
4764 }
4765 } // END of the loop over all the function body lines.
4766
4767 return OK;
4768}
4769
4770/*
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004771 * Returns TRUE if the end of a scope (if, while, for, block) is missing.
4772 * Called after compiling a def function body.
4773 */
4774 static int
4775compile_dfunc_scope_end_missing(cctx_T *cctx)
4776{
4777 if (cctx->ctx_scope == NULL)
4778 return FALSE;
4779
4780 if (cctx->ctx_scope->se_type == IF_SCOPE)
4781 emsg(_(e_missing_endif));
4782 else if (cctx->ctx_scope->se_type == WHILE_SCOPE)
4783 emsg(_(e_missing_endwhile));
4784 else if (cctx->ctx_scope->se_type == FOR_SCOPE)
4785 emsg(_(e_missing_endfor));
4786 else
4787 emsg(_(e_missing_rcurly));
4788
4789 return TRUE;
4790}
4791
4792/*
Yegappan Lakshmananab9a8942024-12-30 09:56:34 +01004793 * When compiling a def function, if it doesn't have an explicit return
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004794 * statement, then generate a default return instruction. For an object
4795 * constructor, return the object.
4796 */
4797 static int
4798compile_dfunc_generate_default_return(ufunc_T *ufunc, cctx_T *cctx)
4799{
4800 // TODO: if a function ends in "throw" but there was a return elsewhere we
4801 // should not assume the return type is "void".
4802 if (cctx->ctx_had_return || cctx->ctx_had_throw)
4803 return OK;
4804
4805 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
4806 ufunc->uf_ret_type = &t_void;
4807 else if (ufunc->uf_ret_type->tt_type != VAR_VOID
4808 && !IS_CONSTRUCTOR_METHOD(ufunc))
4809 {
4810 emsg(_(e_missing_return_statement));
4811 return FAIL;
4812 }
4813
4814 // Return void if there is no return at the end.
4815 // For a constructor return the object.
4816 if (IS_CONSTRUCTOR_METHOD(ufunc))
4817 {
4818 generate_instr(cctx, ISN_RETURN_OBJECT);
4819 ufunc->uf_ret_type = &ufunc->uf_class->class_object_type;
4820 }
4821 else
4822 generate_instr(cctx, ISN_RETURN_VOID);
4823
4824 return OK;
4825}
4826
4827/*
4828 * Perform the chores after successfully compiling a def function.
4829 */
4830 static void
4831compile_dfunc_epilogue(
4832 cctx_T *outer_cctx,
4833 ufunc_T *ufunc,
4834 garray_T *instr,
4835 cctx_T *cctx)
4836{
4837 dfunc_T *dfunc;
4838
4839 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
4840 dfunc->df_deleted = FALSE;
4841 dfunc->df_script_seq = current_sctx.sc_seq;
4842
4843#ifdef FEAT_PROFILE
4844 if (cctx->ctx_compile_type == CT_PROFILE)
4845 {
4846 dfunc->df_instr_prof = instr->ga_data;
4847 dfunc->df_instr_prof_count = instr->ga_len;
4848 }
4849 else
4850#endif
4851 if (cctx->ctx_compile_type == CT_DEBUG)
4852 {
4853 dfunc->df_instr_debug = instr->ga_data;
4854 dfunc->df_instr_debug_count = instr->ga_len;
4855 }
4856 else
4857 {
4858 dfunc->df_instr = instr->ga_data;
4859 dfunc->df_instr_count = instr->ga_len;
4860 }
4861 dfunc->df_varcount = dfunc->df_var_names.ga_len;
4862 dfunc->df_has_closure = cctx->ctx_has_closure;
4863
4864 if (cctx->ctx_outer_used)
4865 {
4866 ufunc->uf_flags |= FC_CLOSURE;
4867 if (outer_cctx != NULL)
4868 ++outer_cctx->ctx_closure_count;
4869 }
4870
4871 ufunc->uf_def_status = UF_COMPILED;
4872}
4873
4874/*
4875 * Perform the cleanup when a def function compilation fails.
4876 */
4877 static void
4878compile_dfunc_ufunc_cleanup(
4879 ufunc_T *ufunc,
4880 garray_T *instr,
4881 int new_def_function,
4882 char *errormsg,
4883 int did_emsg_before,
4884 cctx_T *cctx)
4885{
4886 dfunc_T *dfunc;
4887
4888 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
4889
4890 // Compiling aborted, free the generated instructions.
4891 clear_instr_ga(instr);
4892 VIM_CLEAR(dfunc->df_name);
4893 ga_clear_strings(&dfunc->df_var_names);
4894
4895 // If using the last entry in the table and it was added above, we
4896 // might as well remove it.
4897 if (!dfunc->df_deleted && new_def_function
4898 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
4899 {
4900 --def_functions.ga_len;
4901 ufunc->uf_dfunc_idx = 0;
4902 }
4903 ufunc->uf_def_status = UF_COMPILE_ERROR;
4904
4905 while (cctx->ctx_scope != NULL)
4906 drop_scope(cctx);
4907
4908 if (errormsg != NULL)
4909 emsg(errormsg);
4910 else if (did_emsg == did_emsg_before)
4911 emsg(_(e_compiling_def_function_failed));
4912}
4913
4914/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004915 * After ex_function() has collected all the function lines: parse and compile
4916 * the lines into instructions.
4917 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004918 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
4919 * the return statement (used for lambda). When uf_ret_type is already set
4920 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01004921 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004922 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02004923 * This can be used recursively through compile_lambda(), which may reallocate
4924 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02004925 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004926 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02004927 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01004928compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02004929 ufunc_T *ufunc,
4930 int check_return_type,
4931 compiletype_T compile_type,
4932 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004933{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004934 garray_T lines_to_free;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004935 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004936 cctx_T cctx;
4937 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01004938 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02004939 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004940 int ret = FAIL;
4941 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02004942 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02004943 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02004944 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02004945 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004946
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004947 // allocated lines are freed at the end
4948 ga_init2(&lines_to_free, sizeof(char_u *), 50);
4949
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004950 // Initialize the ufunc and the compilation context
4951 if (compile_dfunc_ufunc_init(ufunc, outer_cctx, compile_type,
4952 &new_def_function) == FAIL)
Bram Moolenaar96923b72022-03-15 15:57:04 +00004953 return FAIL;
Bram Moolenaar96923b72022-03-15 15:57:04 +00004954
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004955 compile_dfunc_cctx_init(&cctx, outer_cctx, ufunc, compile_type);
Bram Moolenaar985116a2020-07-12 17:31:09 +02004956
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004957 instr = &cctx.ctx_instr;
4958
Bram Moolenaar25e0f582020-05-25 22:36:50 +02004959 // Set the context to the function, it may be compiled when called from
4960 // another script. Set the script version to the most modern one.
4961 // The line number will be set in next_line_from_context().
4962 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004963 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4964
Bram Moolenaardc4c2302021-04-25 13:54:42 +02004965 // Don't use the flag from ":legacy" here.
4966 cmdmod.cmod_flags &= ~CMOD_LEGACY;
4967
Bram Moolenaar25e0f582020-05-25 22:36:50 +02004968 // Make sure error messages are OK.
4969 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
4970 if (do_estack_push)
4971 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02004972 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02004973
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004974 // Make sure arguments don't shadow variables in the context
Bram Moolenaar9a015112021-12-31 14:06:45 +00004975 if (check_args_shadowing(ufunc, &cctx) == FAIL)
4976 goto erret;
4977
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004978 // For an object method and a constructor generate instructions to
4979 // initialize "this" and the object variables.
Bram Moolenaar574950d2023-01-03 19:08:50 +00004980 if (ufunc->uf_flags & (FC_OBJECT|FC_NEW))
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004981 if (obj_method_prologue(ufunc, &cctx) == FAIL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004982 goto erret;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004983
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004984 if (ufunc->uf_def_args.ga_len > 0)
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004985 if (compile_def_function_default_args(ufunc, instr, &cctx) == FAIL)
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004986 goto erret;
Bram Moolenaare28d9b32021-07-03 18:56:53 +02004987 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004988
Ernie Rael7c92e882025-01-18 17:26:39 +01004989 // Compiling an abstract method or a function in an interface is done to
4990 // get the function type. No code is actually compiled.
4991 if (ufunc->uf_class != NULL && (IS_INTERFACE(ufunc->uf_class)
4992 || IS_ABSTRACT_METHOD(ufunc)))
Bram Moolenaarb8bebd02023-01-30 20:24:23 +00004993 {
4994 ufunc->uf_def_status = UF_NOT_COMPILED;
4995 ret = OK;
4996 goto erret;
4997 }
4998
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004999 // compile the function body
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01005000 if (compile_def_function_body(ufunc->uf_lines.ga_len, check_return_type,
5001 &lines_to_free, &errormsg, &cctx) == FAIL)
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02005002 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005003
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01005004 if (compile_dfunc_scope_end_missing(&cctx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005005 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005006
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01005007 if (compile_dfunc_generate_default_return(ufunc, &cctx) == FAIL)
5008 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005009
Bram Moolenaar599410c2021-04-10 14:03:43 +02005010 // When compiled with ":silent!" and there was an error don't consider the
5011 // function compiled.
5012 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01005013 compile_dfunc_epilogue(outer_cctx, ufunc, instr, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005014
5015 ret = OK;
5016
5017erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02005018 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005019 {
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01005020 // compilation failed. do cleanup.
5021 compile_dfunc_ufunc_cleanup(ufunc, instr, new_def_function,
5022 errormsg, did_emsg_before, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005023 }
5024
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005025 if (cctx.ctx_redir_lhs.lhs_name != NULL)
5026 {
5027 if (ret == OK)
5028 {
5029 emsg(_(e_missing_redir_end));
5030 ret = FAIL;
5031 }
5032 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02005033 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005034 }
5035
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005036 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02005037 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02005038 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02005039 if (do_estack_push)
5040 estack_pop();
5041
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005042 ga_clear_strings(&lines_to_free);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005043 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005044 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02005045 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005046}
5047
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005048 void
5049set_function_type(ufunc_T *ufunc)
5050{
5051 int varargs = ufunc->uf_va_name != NULL;
5052 int argcount = ufunc->uf_args.ga_len;
5053
5054 // Create a type for the function, with the return type and any
5055 // argument types.
5056 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
5057 // The type is included in "tt_args".
5058 if (argcount > 0 || varargs)
5059 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01005060 if (ufunc->uf_type_list.ga_itemsize == 0)
5061 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005062 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
5063 argcount, &ufunc->uf_type_list);
5064 // Add argument types to the function type.
5065 if (func_type_add_arg_types(ufunc->uf_func_type,
5066 argcount + varargs,
5067 &ufunc->uf_type_list) == FAIL)
5068 return;
5069 ufunc->uf_func_type->tt_argcount = argcount + varargs;
5070 ufunc->uf_func_type->tt_min_argcount =
5071 argcount - ufunc->uf_def_args.ga_len;
5072 if (ufunc->uf_arg_types == NULL)
5073 {
5074 int i;
5075
5076 // lambda does not have argument types.
5077 for (i = 0; i < argcount; ++i)
5078 ufunc->uf_func_type->tt_args[i] = &t_any;
5079 }
5080 else
5081 mch_memmove(ufunc->uf_func_type->tt_args,
5082 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
5083 if (varargs)
5084 {
5085 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +02005086 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005087 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
5088 }
5089 }
5090 else
5091 // No arguments, can use a predefined type.
5092 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
5093 argcount, &ufunc->uf_type_list);
5094}
5095
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005096/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01005097 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01005098 */
5099 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005100delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01005101{
5102 int idx;
5103
Bram Moolenaard505d172022-12-18 21:42:55 +00005104 // In same cases the instructions may refer to a class in which the
5105 // function is defined and unreferencing the class may call back here
5106 // recursively. Set the df_delete_busy to avoid problems.
5107 if (dfunc->df_delete_busy)
5108 return;
5109 dfunc->df_delete_busy = TRUE;
5110
Bram Moolenaar20431c92020-03-20 18:39:46 +01005111 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005112 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005113
5114 if (dfunc->df_instr != NULL)
5115 {
5116 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
5117 delete_instr(dfunc->df_instr + idx);
5118 VIM_CLEAR(dfunc->df_instr);
5119 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +02005120 if (dfunc->df_instr_debug != NULL)
5121 {
5122 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
5123 delete_instr(dfunc->df_instr_debug + idx);
5124 VIM_CLEAR(dfunc->df_instr_debug);
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +02005125 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +01005126#ifdef FEAT_PROFILE
5127 if (dfunc->df_instr_prof != NULL)
5128 {
5129 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
5130 delete_instr(dfunc->df_instr_prof + idx);
5131 VIM_CLEAR(dfunc->df_instr_prof);
Bram Moolenaarc05fe072021-01-24 21:30:48 +01005132 }
5133#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +01005134
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005135 if (mark_deleted)
5136 dfunc->df_deleted = TRUE;
5137 if (dfunc->df_ufunc != NULL)
5138 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaard505d172022-12-18 21:42:55 +00005139
5140 dfunc->df_delete_busy = FALSE;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005141}
5142
5143/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005144 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01005145 * function, unless another user function still uses it.
5146 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005147 */
5148 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01005149unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005150{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00005151 if (ufunc->uf_dfunc_idx <= 0)
5152 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005153
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00005154 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5155 + ufunc->uf_dfunc_idx;
5156
5157 if (--dfunc->df_refcount <= 0)
5158 delete_def_function_contents(dfunc, TRUE);
5159 ufunc->uf_def_status = UF_NOT_COMPILED;
5160 ufunc->uf_dfunc_idx = 0;
5161 if (dfunc->df_ufunc == ufunc)
5162 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005163}
5164
Bram Moolenaarfdeab652020-09-19 15:16:50 +02005165/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01005166 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02005167 */
5168 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01005169link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02005170{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00005171 if (ufunc->uf_dfunc_idx <= 0)
5172 return;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02005173
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00005174 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5175 + ufunc->uf_dfunc_idx;
5176
5177 ++dfunc->df_refcount;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02005178}
5179
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005180#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01005181/*
5182 * Free all functions defined with ":def".
5183 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005184 void
5185free_def_functions(void)
5186{
Bram Moolenaar20431c92020-03-20 18:39:46 +01005187 int idx;
5188
5189 for (idx = 0; idx < def_functions.ga_len; ++idx)
5190 {
5191 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
5192
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005193 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01005194 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005195 }
5196
5197 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005198}
5199#endif
5200
5201
5202#endif // FEAT_EVAL