blob: 131bab7f47fabbcd8b7723ece0843bfcc6ed260a [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 {
1038 emsg(_(e_cannot_use_bang_with_nested_def));
1039 return NULL;
1040 }
1041
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001042 if (*name_start == '/')
1043 {
1044 name_end = skip_regexp(name_start + 1, '/', TRUE);
1045 if (*name_end == '/')
1046 ++name_end;
Bram Moolenaar63b91732021-08-05 20:40:03 +02001047 set_nextcmd(eap, name_end);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001048 }
1049 if (name_end == name_start || *skipwhite(name_end) != '(')
1050 {
1051 if (!ends_excmd2(name_start, name_end))
1052 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00001053 if (*skipwhite(name_end) == '.')
1054 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
1055 eap->cmd);
1056 else
1057 semsg(_(e_invalid_command_str), eap->cmd);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001058 return NULL;
1059 }
1060
1061 // "def" or "def Name": list functions
1062 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
1063 return NULL;
1064 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
1065 }
1066
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02001067 // Only g:Func() can use a namespace.
1068 if (name_start[1] == ':' && !is_global)
1069 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001070 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02001071 return NULL;
1072 }
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00001073 if (cctx->ctx_skip != SKIP_YES
1074 && check_defined(name_start, name_end - name_start, cctx,
Bram Moolenaardce24412022-02-08 20:35:30 +00001075 NULL, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02001076 return NULL;
Bram Moolenaarf681cfb2022-02-07 20:30:57 +00001077 if (!ASCII_ISUPPER(is_global ? name_start[2] : name_start[0]))
1078 {
Bram Moolenaar3787f262022-02-07 21:54:01 +00001079 semsg(_(e_function_name_must_start_with_capital_str), name_start);
Bram Moolenaarf681cfb2022-02-07 20:30:57 +00001080 return NULL;
1081 }
Bram Moolenaareef21022020-08-01 22:16:43 +02001082
Bram Moolenaar04b12692020-05-04 23:24:44 +02001083 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001084 fill_exarg_from_cctx(eap, cctx);
1085
Bram Moolenaar04b12692020-05-04 23:24:44 +02001086 eap->forceit = FALSE;
Bram Moolenaar38453522021-11-28 22:00:12 +00001087 // We use the special <Lamba>99 name, but it's not really a lambda.
John Marriottb32800f2025-02-01 15:25:34 +01001088 lambda_name = get_lambda_name();
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +01001089 lambda_name.string = vim_strnsave(lambda_name.string, lambda_name.length);
1090 if (lambda_name.string == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001091 return NULL;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001092
1093 // This may free the current line, make a copy of the name.
1094 off = is_global ? 2 : 0;
1095 func_name = vim_strnsave(name_start + off, name_end - name_start - off);
1096 if (func_name == NULL)
1097 {
1098 r = FAIL;
1099 goto theend;
1100 }
1101
Bram Moolenaar39c82ea2023-01-02 13:08:01 +00001102 // Make sure "KeyTyped" is not set, it may cause indent to be written.
1103 int save_KeyTyped = KeyTyped;
1104 KeyTyped = FALSE;
1105
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +01001106 ufunc = define_function(eap, lambda_name.string, lines_to_free, 0, NULL, 0);
Bram Moolenaar39c82ea2023-01-02 13:08:01 +00001107
1108 KeyTyped = save_KeyTyped;
1109
Bram Moolenaar822ba242020-05-24 23:00:18 +02001110 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001111 {
1112 r = eap->skip ? OK : FAIL;
1113 goto theend;
1114 }
Bram Moolenaar7473a842021-12-28 17:55:26 +00001115 if (eap->nextcmd != NULL)
1116 {
1117 semsg(_(e_text_found_after_str_str),
1118 eap->cmdidx == CMD_def ? "enddef" : "endfunction", eap->nextcmd);
1119 r = FAIL;
Bram Moolenaard2939812021-12-30 17:09:05 +00001120 func_ptr_unref(ufunc);
Bram Moolenaar7473a842021-12-28 17:55:26 +00001121 goto theend;
1122 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01001123
1124 // copy over the block scope IDs before compiling
1125 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
1126 {
1127 int block_depth = cctx->ctx_ufunc->uf_block_depth;
1128
1129 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
1130 if (ufunc->uf_block_ids != NULL)
1131 {
1132 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
1133 sizeof(int) * block_depth);
1134 ufunc->uf_block_depth = block_depth;
1135 }
1136 }
1137
Bram Moolenaara915fa02022-03-23 11:29:15 +00001138 // Define the funcref before compiling, so that it is found by any
1139 // recursive call.
1140 if (is_global)
1141 {
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +01001142 r = generate_NEWFUNC(cctx, lambda_name.string, func_name);
Bram Moolenaara915fa02022-03-23 11:29:15 +00001143 func_name = NULL;
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +01001144 lambda_name.string = NULL;
1145 lambda_name.length = 0;
Bram Moolenaara915fa02022-03-23 11:29:15 +00001146 }
1147 else
1148 {
1149 // Define a local variable for the function reference.
Bram Moolenaar1889f492022-08-16 19:34:44 +01001150 lvar = reserve_local(cctx, func_name, name_end - name_start,
Bram Moolenaar6586a012022-09-30 11:04:50 +01001151 ASSIGN_CONST, ufunc->uf_func_type);
Bram Moolenaara915fa02022-03-23 11:29:15 +00001152 if (lvar == NULL)
1153 goto theend;
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001154 if (generate_FUNCREF(cctx, ufunc, NULL, FALSE, 0, &funcref_isn_idx) == FAIL)
Bram Moolenaara915fa02022-03-23 11:29:15 +00001155 goto theend;
1156 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
1157 }
1158
Bram Moolenaar139575d2022-03-15 19:29:30 +00001159 compile_type = get_compile_type(ufunc);
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02001160#ifdef FEAT_PROFILE
1161 // If the outer function is profiled, also compile the nested function for
1162 // profiling.
1163 if (cctx->ctx_compile_type == CT_PROFILE)
1164 compile_type = CT_PROFILE;
1165#endif
1166 if (func_needs_compiling(ufunc, compile_type)
1167 && compile_def_function(ufunc, TRUE, compile_type, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02001168 {
1169 func_ptr_unref(ufunc);
Bram Moolenaar1889f492022-08-16 19:34:44 +01001170 if (lvar != NULL)
1171 // Now the local variable can't be used.
1172 *lvar->lv_name = '/'; // impossible value
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001173 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02001174 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02001175
Bram Moolenaar648594e2021-07-11 17:55:01 +02001176#ifdef FEAT_PROFILE
1177 // When the outer function is compiled for profiling, the nested function
1178 // may be called without profiling. Compile it here in the right context.
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02001179 if (compile_type == CT_PROFILE && func_needs_compiling(ufunc, CT_NONE))
Bram Moolenaar648594e2021-07-11 17:55:01 +02001180 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1181#endif
1182
Bram Moolenaara915fa02022-03-23 11:29:15 +00001183 // If a FUNCREF instruction was generated, set the index after compiling.
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001184 if (funcref_isn_idx != -1 && ufunc->uf_def_status == UF_COMPILED)
1185 {
1186 isn_T *funcref_isn = ((isn_T *)cctx->ctx_instr.ga_data) +
1187 funcref_isn_idx;
Bram Moolenaara915fa02022-03-23 11:29:15 +00001188 funcref_isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001189 }
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001190
1191theend:
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +01001192 vim_free(lambda_name.string);
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001193 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001194 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02001195}
1196
1197/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001198 * Compile one Vim expression {expr} in string "p".
1199 * "p" points to the opening "{".
1200 * Return a pointer to the character after "}", NULL for an error.
1201 */
1202 char_u *
1203compile_one_expr_in_str(char_u *p, cctx_T *cctx)
1204{
1205 char_u *block_start;
1206 char_u *block_end;
1207
1208 // Skip the opening {.
1209 block_start = skipwhite(p + 1);
1210 block_end = block_start;
1211 if (*block_start != NUL && skip_expr(&block_end, NULL) == FAIL)
1212 return NULL;
1213 block_end = skipwhite(block_end);
1214 // The block must be closed by a }.
1215 if (*block_end != '}')
1216 {
1217 semsg(_(e_missing_close_curly_str), p);
1218 return NULL;
1219 }
1220 if (compile_expr0(&block_start, cctx) == FAIL)
1221 return NULL;
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02001222 may_generate_2STRING(-1, TOSTRING_INTERPOLATE, cctx);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001223
1224 return block_end + 1;
1225}
1226
1227/*
LemonBoy2eaef102022-05-06 13:14:50 +01001228 * Compile a string "str" (either containing a literal string or a mix of
1229 * literal strings and Vim expressions of the form `{expr}`). This is used
1230 * when compiling a heredoc assignment to a variable or an interpolated string
1231 * in a Vim9 def function. Vim9 instructions are generated to push strings,
1232 * evaluate expressions, concatenate them and create a list of lines. When
1233 * "evalstr" is TRUE, Vim expressions in "str" are evaluated.
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001234 */
1235 int
LemonBoy2eaef102022-05-06 13:14:50 +01001236compile_all_expr_in_str(char_u *str, int evalstr, cctx_T *cctx)
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001237{
LemonBoy2eaef102022-05-06 13:14:50 +01001238 char_u *p = str;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001239 char_u *val;
LemonBoy2eaef102022-05-06 13:14:50 +01001240 int count = 0;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001241
1242 if (cctx->ctx_skip == SKIP_YES)
1243 return OK;
1244
LemonBoy2eaef102022-05-06 13:14:50 +01001245 if (!evalstr || *str == NUL)
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001246 {
LemonBoy2eaef102022-05-06 13:14:50 +01001247 // Literal string, possibly empty.
1248 val = *str != NUL ? vim_strsave(str) : NULL;
1249 return generate_PUSHS(cctx, &val);
1250 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001251
LemonBoy2eaef102022-05-06 13:14:50 +01001252 // Push all the string pieces to the stack, followed by a ISN_CONCAT.
1253 while (*p != NUL)
1254 {
1255 char_u *lit_start;
LemonBoy2eaef102022-05-06 13:14:50 +01001256 int escaped_brace = FALSE;
1257
1258 // Look for a block start.
1259 lit_start = p;
1260 while (*p != '{' && *p != '}' && *p != NUL)
1261 ++p;
1262
1263 if (*p != NUL && *p == p[1])
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001264 {
LemonBoy2eaef102022-05-06 13:14:50 +01001265 // Escaped brace, unescape and continue.
1266 // Include the brace in the literal string.
1267 ++p;
1268 escaped_brace = TRUE;
1269 }
1270 else if (*p == '}')
1271 {
1272 semsg(_(e_stray_closing_curly_str), str);
1273 return FAIL;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001274 }
LemonBoy372bcce2022-04-25 12:43:20 +01001275
LemonBoy2eaef102022-05-06 13:14:50 +01001276 // Append the literal part.
1277 if (p != lit_start)
1278 {
1279 val = vim_strnsave(lit_start, (size_t)(p - lit_start));
1280 if (generate_PUSHS(cctx, &val) == FAIL)
1281 return FAIL;
1282 ++count;
1283 }
1284
1285 if (*p == NUL)
1286 break;
1287
1288 if (escaped_brace)
1289 {
1290 // Skip the second brace.
1291 ++p;
1292 continue;
1293 }
1294
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001295 p = compile_one_expr_in_str(p, cctx);
1296 if (p == NULL)
LemonBoy2eaef102022-05-06 13:14:50 +01001297 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01001298 ++count;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001299 }
LemonBoy2eaef102022-05-06 13:14:50 +01001300
1301 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001302 if (count > 1)
LemonBoy2eaef102022-05-06 13:14:50 +01001303 return generate_CONCAT(cctx, count);
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001304
1305 return OK;
1306}
1307
1308/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001309 * Return the length of an assignment operator, or zero if there isn't one.
1310 */
1311 int
1312assignment_len(char_u *p, int *heredoc)
1313{
1314 if (*p == '=')
1315 {
1316 if (p[1] == '<' && p[2] == '<')
1317 {
1318 *heredoc = TRUE;
1319 return 3;
1320 }
1321 return 1;
1322 }
1323 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
1324 return 2;
1325 if (STRNCMP(p, "..=", 3) == 0)
1326 return 3;
1327 return 0;
1328}
1329
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001330/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001331 * Generate the load instruction for "name".
1332 */
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001333 static int
Bram Moolenaard505d172022-12-18 21:42:55 +00001334generate_loadvar(cctx_T *cctx, lhs_T *lhs)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001335{
Bram Moolenaard505d172022-12-18 21:42:55 +00001336 char_u *name = lhs->lhs_name;
1337 type_T *type = lhs->lhs_type;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001338 int res = OK;
Bram Moolenaard505d172022-12-18 21:42:55 +00001339
1340 switch (lhs->lhs_dest)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001341 {
1342 case dest_option:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001343 case dest_func_option:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001344 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1345 break;
1346 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01001347 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001348 {
1349 if (name[2] == NUL)
1350 generate_instr_type(cctx, ISN_LOADGDICT, &t_dict_any);
1351 else
1352 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
1353 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01001354 else
1355 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001356 break;
1357 case dest_buffer:
1358 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
1359 break;
1360 case dest_window:
1361 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
1362 break;
1363 case dest_tab:
1364 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
1365 break;
1366 case dest_script:
Ernie Rael3f821d62024-04-24 20:07:50 +02001367 case dest_script_v9:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001368 res = compile_load_scriptvar(cctx,
Hirohito Higashi6c012832025-02-25 20:29:50 +01001369 name + (name[1] == ':' ? 2 : 0), NULL, NULL, NULL);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001370 break;
1371 case dest_env:
1372 // Include $ in the name here
1373 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
1374 break;
1375 case dest_reg:
1376 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
1377 break;
1378 case dest_vimvar:
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001379 generate_LOADV(cctx, name + 2);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001380 break;
1381 case dest_local:
Bram Moolenaar4875d6a2022-08-17 15:55:51 +01001382 if (cctx->ctx_skip != SKIP_YES)
1383 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001384 lvar_T *lvar = lhs->lhs_lvar;
Bram Moolenaar4875d6a2022-08-17 15:55:51 +01001385 if (lvar->lv_from_outer > 0)
1386 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
Bram Moolenaar65449bd2022-09-19 16:02:43 +01001387 lvar->lv_loop_depth, lvar->lv_loop_idx, type);
Bram Moolenaar4875d6a2022-08-17 15:55:51 +01001388 else
1389 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
1390 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001391 break;
Bram Moolenaard505d172022-12-18 21:42:55 +00001392 case dest_class_member:
1393 generate_CLASSMEMBER(cctx, TRUE, lhs->lhs_class,
1394 lhs->lhs_classmember_idx);
1395 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01001396 case dest_expr:
1397 // list or dict value should already be on the stack.
1398 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001399 }
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001400
1401 return res;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001402}
1403
Bram Moolenaardc234ca2020-11-28 18:52:33 +01001404/*
1405 * Skip over "[expr]" or ".member".
1406 * Does not check for any errors.
1407 */
1408 static char_u *
1409skip_index(char_u *start)
1410{
1411 char_u *p = start;
1412
1413 if (*p == '[')
1414 {
1415 p = skipwhite(p + 1);
1416 (void)skip_expr(&p, NULL);
1417 p = skipwhite(p);
1418 if (*p == ']')
1419 return p + 1;
1420 return p;
1421 }
1422 // if (*p == '.')
1423 return to_name_end(p + 1, TRUE);
1424}
1425
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001426 void
1427vim9_declare_error(char_u *name)
1428{
1429 char *scope = "";
1430
1431 switch (*name)
1432 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02001433 case 'g': scope = _("global"); break;
1434 case 'b': scope = _("buffer"); break;
1435 case 'w': scope = _("window"); break;
1436 case 't': scope = _("tab"); break;
1437 case 'v': scope = "v:"; break;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001438 case '$': semsg(_(e_cannot_declare_an_environment_variable_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001439 return;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001440 case '&': semsg(_(e_cannot_declare_an_option_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001441 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001442 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001443 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02001444 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001445 }
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001446 semsg(_(e_cannot_declare_a_scope_variable_str), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001447}
1448
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001449/*
Bram Moolenaar3558afe2022-10-13 16:12:57 +01001450 * Return TRUE if "name" is a valid register to use.
1451 * Return FALSE and give an error message if not.
1452 */
1453 static int
1454valid_dest_reg(int name)
1455{
1456 if ((name == '@' || valid_yank_reg(name, FALSE)) && name != '.')
1457 return TRUE;
1458 emsg_invreg(name);
1459 return FAIL;
1460}
1461
1462/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001463 * For one assignment figure out the type of destination. Return it in "dest".
1464 * When not recognized "dest" is not set.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001465 * For an option "option_scope" is set.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001466 * For a v:var "vimvaridx" is set.
1467 * "type" is set to the destination type if known, unchanted otherwise.
1468 * Return FAIL if an error message was given.
1469 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001470 int
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001471get_var_dest(
1472 char_u *name,
1473 assign_dest_T *dest,
Bram Moolenaar97f8c102022-04-02 19:43:57 +01001474 cmdidx_T cmdidx,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001475 int *option_scope,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001476 int *vimvaridx,
1477 type_T **type,
1478 cctx_T *cctx)
1479{
1480 char_u *p;
1481
1482 if (*name == '&')
1483 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001484 int cc;
1485 long numval;
1486 getoption_T opt_type;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001487 int opt_p_flags;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001488
1489 *dest = dest_option;
1490 if (cmdidx == CMD_final || cmdidx == CMD_const)
1491 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001492 emsg(_(e_cannot_lock_option));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001493 return FAIL;
1494 }
1495 p = name;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001496 p = find_option_end(&p, option_scope);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001497 if (p == NULL)
1498 {
1499 // cannot happen?
Bram Moolenaar108010a2021-06-27 22:03:33 +02001500 emsg(_(e_unexpected_characters_in_assignment));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001501 return FAIL;
1502 }
1503 cc = *p;
1504 *p = NUL;
1505 opt_type = get_option_value(skip_option_env_lead(name),
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001506 &numval, NULL, &opt_p_flags, *option_scope);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001507 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001508 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001509 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001510 case gov_unknown:
Bram Moolenaare1242042021-12-16 20:56:57 +00001511 semsg(_(e_unknown_option_str), name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001512 return FAIL;
1513 case gov_string:
1514 case gov_hidden_string:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001515 if (opt_p_flags & P_FUNC)
1516 {
1517 // might be a Funcref, check the type later
1518 *type = &t_any;
1519 *dest = dest_func_option;
1520 }
1521 else
1522 {
1523 *type = &t_string;
1524 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001525 break;
1526 case gov_bool:
1527 case gov_hidden_bool:
1528 *type = &t_bool;
1529 break;
1530 case gov_number:
1531 case gov_hidden_number:
1532 *type = &t_number;
1533 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001534 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001535 }
1536 else if (*name == '$')
1537 {
1538 *dest = dest_env;
1539 *type = &t_string;
1540 }
1541 else if (*name == '@')
1542 {
Bram Moolenaar3558afe2022-10-13 16:12:57 +01001543 if (!valid_dest_reg(name[1]))
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001544 return FAIL;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001545 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02001546 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001547 }
1548 else if (STRNCMP(name, "g:", 2) == 0)
1549 {
1550 *dest = dest_global;
1551 }
1552 else if (STRNCMP(name, "b:", 2) == 0)
1553 {
1554 *dest = dest_buffer;
1555 }
1556 else if (STRNCMP(name, "w:", 2) == 0)
1557 {
1558 *dest = dest_window;
1559 }
1560 else if (STRNCMP(name, "t:", 2) == 0)
1561 {
1562 *dest = dest_tab;
1563 }
1564 else if (STRNCMP(name, "v:", 2) == 0)
1565 {
1566 typval_T *vtv;
1567 int di_flags;
1568
1569 *vimvaridx = find_vim_var(name + 2, &di_flags);
1570 if (*vimvaridx < 0)
1571 {
1572 semsg(_(e_variable_not_found_str), name);
1573 return FAIL;
1574 }
1575 // We use the current value of "sandbox" here, is that OK?
1576 if (var_check_ro(di_flags, name, FALSE))
1577 return FAIL;
1578 *dest = dest_vimvar;
1579 vtv = get_vim_var_tv(*vimvaridx);
1580 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
1581 }
1582 return OK;
1583}
1584
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001585 static int
Bram Moolenaar97f8c102022-04-02 19:43:57 +01001586is_decl_command(cmdidx_T cmdidx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001587{
1588 return cmdidx == CMD_let || cmdidx == CMD_var
1589 || cmdidx == CMD_final || cmdidx == CMD_const;
1590}
1591
1592/*
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02001593 * Returns TRUE if the class or object variable in "lhs" is modifiable.
1594 * "var_start" points to the start of the variable name and "lhs->lhs_varlen"
1595 * has the total length. Note that the "lhs" can be nested an object reference
1596 * (e.g. a.b.c.d.var).
1597 */
1598 static int
1599lhs_class_member_modifiable(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
1600{
1601 size_t varlen = lhs->lhs_varlen;
1602 class_T *cl = lhs->lhs_type->tt_class;
1603 int is_object = lhs->lhs_type->tt_type == VAR_OBJECT;
1604 char_u *name = var_start + varlen + 1;
1605 size_t namelen = lhs->lhs_end - var_start - varlen - 1;
1606 ocmember_T *m;
1607
1608 m = member_lookup(cl, lhs->lhs_type->tt_type, name, namelen, NULL);
1609 if (m == NULL)
1610 {
1611 member_not_found_msg(cl, lhs->lhs_type->tt_type, name, namelen);
1612 return FALSE;
1613 }
1614
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01001615 if (IS_ENUM(cl))
1616 {
1617 semsg(_(e_enumvalue_str_cannot_be_modified), cl->class_name,
1618 m->ocm_name);
1619 return FALSE;
1620 }
1621
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02001622 // If it is private member variable, then accessing it outside the
1623 // class is not allowed.
1624 // If it is a read only class variable, then it can be modified
1625 // only inside the class where it is defined.
1626 if ((m->ocm_access != VIM_ACCESS_ALL) &&
1627 ((is_object && !inside_class(cctx, cl))
1628 || (!is_object && cctx->ctx_ufunc->uf_class != cl)))
1629 {
1630 char *msg = (m->ocm_access == VIM_ACCESS_PRIVATE)
Ernie Rael03042a22023-11-11 08:53:32 +01001631 ? e_cannot_access_protected_variable_str
RestorerZ7fe8f432023-09-24 23:21:24 +02001632 : e_variable_is_not_writable_str;
Ernie Raele6c9aa52023-10-06 19:55:52 +02001633 emsg_var_cl_define(msg, m->ocm_name, 0, cl);
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02001634 return FALSE;
1635 }
1636
1637 return TRUE;
1638}
1639
1640/*
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001641 * Initialize "lhs" with default values
Bram Moolenaar752fc692021-01-04 21:57:11 +01001642 */
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001643 static void
1644lhs_init_defaults(lhs_T *lhs)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001645{
Bram Moolenaar752fc692021-01-04 21:57:11 +01001646 CLEAR_POINTER(lhs);
1647 lhs->lhs_dest = dest_local;
1648 lhs->lhs_vimvaridx = -1;
1649 lhs->lhs_scriptvar_idx = -1;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001650 lhs->lhs_member_idx = -1;
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001651}
Bram Moolenaar752fc692021-01-04 21:57:11 +01001652
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001653/*
1654 * When compiling a LHS variable name, find the end of the destination and the
1655 * end of the variable name.
1656 */
1657 static int
1658lhs_find_var_end(
1659 lhs_T *lhs,
1660 char_u *var_start,
1661 int is_decl,
1662 char_u **var_endp)
1663{
1664 char_u *var_end = *var_endp;
1665
1666 // "lhs_dest_end" is the end of the destination, including "[expr]" or
Bram Moolenaar752fc692021-01-04 21:57:11 +01001667 // ".name".
1668 // "var_end" is the end of the variable/option/etc. name.
1669 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
1670 if (*var_start == '@')
Bram Moolenaar3558afe2022-10-13 16:12:57 +01001671 {
1672 if (!valid_dest_reg(var_start[1]))
1673 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001674 var_end = var_start + 2;
Bram Moolenaar3558afe2022-10-13 16:12:57 +01001675 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01001676 else
1677 {
1678 // skip over the leading "&", "&l:", "&g:" and "$"
1679 var_end = skip_option_env_lead(var_start);
1680 var_end = to_name_end(var_end, TRUE);
1681 }
1682
1683 // "a: type" is declaring variable "a" with a type, not dict "a:".
1684 if (is_decl && lhs->lhs_dest_end == var_start + 2
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001685 && lhs->lhs_dest_end[-1] == ':')
Bram Moolenaar752fc692021-01-04 21:57:11 +01001686 --lhs->lhs_dest_end;
1687 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
1688 --var_end;
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001689
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00001690 lhs->lhs_end = lhs->lhs_dest_end;
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001691 *var_endp = var_end;
1692
1693 return OK;
1694}
1695
1696/*
1697 * Set various fields in "lhs"
1698 */
1699 static int
1700lhs_init(
1701 lhs_T *lhs,
1702 char_u *var_start,
1703 int is_decl,
1704 int heredoc,
1705 char_u **var_endp)
1706{
1707 char_u *var_end = *var_endp;
1708
1709 lhs_init_defaults(lhs);
1710
1711 // Find the end of the variable and the destination
1712 if (lhs_find_var_end(lhs, var_start, is_decl, &var_end) == FAIL)
1713 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001714
1715 // compute the length of the destination without "[expr]" or ".name"
1716 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02001717 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001718 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
1719 if (lhs->lhs_name == NULL)
1720 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01001721
1722 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
1723 // Something follows after the variable: "var[idx]" or "var.key".
1724 lhs->lhs_has_index = TRUE;
1725
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001726 lhs->lhs_type = heredoc ? &t_list_string : &t_any;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001727
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001728 *var_endp = var_end;
1729
1730 return OK;
1731}
1732
1733/*
1734 * Compile a LHS class variable name.
1735 */
1736 static int
1737compile_lhs_class_variable(
1738 cctx_T *cctx,
1739 lhs_T *lhs,
1740 class_T *defcl,
1741 int is_decl)
1742{
1743 if (cctx->ctx_ufunc->uf_defclass != defcl)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001744 {
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001745 // A class variable can be accessed without the class name
1746 // only inside a class.
1747 semsg(_(e_class_variable_str_accessible_only_inside_class_str),
1748 lhs->lhs_name, defcl->class_name);
1749 return FAIL;
1750 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01001751
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001752 if (is_decl)
1753 {
1754 semsg(_(e_variable_already_declared_in_class_str), lhs->lhs_name);
1755 return FAIL;
1756 }
1757
1758 ocmember_T *m = &defcl->class_class_members[lhs->lhs_classmember_idx];
1759 if (oc_var_check_ro(defcl, m))
1760 return FAIL;
1761
1762 lhs->lhs_dest = dest_class_member;
1763 // The class variable is defined either in the current class or
1764 // in one of the parent class in the hierarchy.
1765 lhs->lhs_class = defcl;
1766 lhs->lhs_type = oc_member_type_by_idx(defcl, FALSE,
1767 lhs->lhs_classmember_idx);
1768
1769 return OK;
1770}
1771
1772/*
1773 * Compile an imported LHS variable
1774 */
1775 static int
1776compile_lhs_import_var(
1777 lhs_T *lhs,
1778 imported_T *import,
1779 char_u *var_start,
1780 char_u **var_endp,
1781 char_u **rawnamep)
1782{
1783 char_u *var_end = *var_endp;
1784 char_u *dot = vim_strchr(var_start, '.');
1785 char_u *p;
1786
1787 // for an import the name is what comes after the dot
1788 if (dot == NULL)
1789 {
1790 semsg(_(e_no_dot_after_imported_name_str), var_start);
1791 return FAIL;
1792 }
1793
1794 p = skipwhite(dot + 1);
1795 var_end = to_name_end(p, TRUE);
1796 if (var_end == p)
1797 {
1798 semsg(_(e_missing_name_after_imported_name_str), var_start);
1799 return FAIL;
1800 }
1801
1802 vim_free(lhs->lhs_name);
1803 lhs->lhs_varlen = var_end - p;
1804 lhs->lhs_name = vim_strnsave(p, lhs->lhs_varlen);
1805 if (lhs->lhs_name == NULL)
1806 return FAIL;
1807 *rawnamep = lhs->lhs_name;
1808 lhs->lhs_scriptvar_sid = import->imp_sid;
1809
1810 // TODO: where do we check this name is exported?
1811
1812 // Check if something follows: "exp.var[idx]" or
1813 // "exp.var.key".
1814 lhs->lhs_has_index = lhs->lhs_dest_end > skipwhite(var_end);
1815
1816 *var_endp = var_end;
1817
1818 return OK;
1819}
1820
1821/*
1822 * Process a script-local variable when compiling a LHS variable name.
1823 */
1824 static int
1825compile_lhs_script_var(
1826 cctx_T *cctx,
1827 lhs_T *lhs,
1828 char_u *var_start,
1829 char_u *var_end,
1830 int is_decl)
1831{
1832 int script_namespace = FALSE;
1833 int script_var = FALSE;
1834 imported_T *import;
1835 char_u *var_name;
Yegappan Lakshmanan084529c2024-12-24 09:50:01 +01001836 size_t var_name_len;
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001837
1838 if (lhs->lhs_varlen > 1 && STRNCMP(var_start, "s:", 2) == 0)
1839 script_namespace = TRUE;
1840
1841 if (script_namespace)
1842 {
1843 var_name = var_start + 2;
1844 var_name_len = lhs->lhs_varlen - 2;
1845 }
1846 else
1847 {
1848 var_name = var_start;
1849 var_name_len = lhs->lhs_varlen;
1850 }
1851
1852 if (script_var_exists(var_name, var_name_len, cctx, NULL) == OK)
1853 script_var = TRUE;
1854
1855 import = find_imported(var_start, lhs->lhs_varlen, FALSE);
1856
1857 if (script_namespace || script_var || import != NULL)
1858 {
1859 char_u *rawname = lhs->lhs_name + (lhs->lhs_name[1] == ':' ? 2 : 0);
1860
1861 if (script_namespace && current_script_is_vim9())
Bram Moolenaar752fc692021-01-04 21:57:11 +01001862 {
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001863 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), var_start);
1864 return FAIL;
1865 }
1866
1867 if (is_decl)
1868 {
1869 if (script_namespace)
1870 semsg(_(e_cannot_declare_script_variable_in_function_str),
1871 lhs->lhs_name);
1872 else
1873 semsg(_(e_variable_already_declared_in_script_str),
1874 lhs->lhs_name);
1875 return FAIL;
1876 }
1877 else if (cctx->ctx_ufunc->uf_script_ctx_version == SCRIPT_VERSION_VIM9
1878 && script_namespace
1879 && !script_var && import == NULL)
1880 {
1881 semsg(_(e_unknown_variable_str), lhs->lhs_name);
1882 return FAIL;
1883 }
1884
1885 lhs->lhs_dest = current_script_is_vim9() ? dest_script_v9 :
1886 dest_script;
1887
1888 // existing script-local variables should have a type
1889 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
1890 if (import != NULL)
1891 {
1892 if (compile_lhs_import_var(lhs, import, var_start, &var_end,
1893 &rawname) == FAIL)
1894 return FAIL;
1895 }
1896
1897 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
1898 {
1899 // Check writable only when no index follows.
1900 lhs->lhs_scriptvar_idx = get_script_item_idx(
1901 lhs->lhs_scriptvar_sid, rawname,
1902 lhs->lhs_has_index ? ASSIGN_FINAL :
1903 ASSIGN_CONST, cctx, NULL);
1904 if (lhs->lhs_scriptvar_idx >= 0)
1905 {
1906 scriptitem_T *si = SCRIPT_ITEM(lhs->lhs_scriptvar_sid);
1907 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1908 + lhs->lhs_scriptvar_idx;
1909
1910 lhs->lhs_type = sv->sv_type;
1911 }
1912 }
1913
1914 return OK;
1915 }
1916
1917 return check_defined(var_start, lhs->lhs_varlen, cctx, NULL, FALSE);
1918}
1919
1920/*
1921 * Compile the LHS destination.
1922 */
1923 static int
1924compile_lhs_var_dest(
1925 cctx_T *cctx,
1926 lhs_T *lhs,
1927 int cmdidx,
1928 char_u *var_start,
1929 char_u *var_end,
1930 int is_decl)
1931{
1932 int declare_error = FALSE;
1933
1934 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
1935 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
1936 &lhs->lhs_type, cctx) == FAIL)
1937 return FAIL;
1938
1939 if (lhs->lhs_dest != dest_local && cmdidx != CMD_const
1940 && cmdidx != CMD_final)
1941 {
1942 // Specific kind of variable recognized.
1943 declare_error = is_decl;
1944 }
1945 else
1946 {
1947 class_T *defcl;
1948
1949 // No specific kind of variable recognized, just a name.
1950 if (check_reserved_name(lhs->lhs_name, lhs->lhs_has_index
1951 && *var_end == '.') == FAIL)
1952 return FAIL;
1953
1954 if (lookup_local(var_start, lhs->lhs_varlen, &lhs->lhs_local_lvar,
1955 cctx) == OK)
1956 {
1957 lhs->lhs_lvar = &lhs->lhs_local_lvar;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001958 }
1959 else
1960 {
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001961 CLEAR_FIELD(lhs->lhs_arg_lvar);
1962 if (arg_exists(var_start, lhs->lhs_varlen,
1963 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
1964 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001965 {
1966 if (is_decl)
1967 {
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001968 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
Bram Moolenaar752fc692021-01-04 21:57:11 +01001969 return FAIL;
1970 }
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001971 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001972 }
1973 }
1974
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001975 if (lhs->lhs_lvar != NULL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001976 {
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001977 if (is_decl)
1978 {
1979 // if we come here with what looks like an assignment like
1980 // .= but which has been rejected by assignment_len() from
1981 // may_compile_assignment give a better error message
1982 char_u *p = skipwhite(lhs->lhs_end);
1983 if (p[0] == '.' && p[1] == '=')
1984 emsg(_(e_dot_equal_not_supported_with_script_version_two));
1985 else if (p[0] == ':')
1986 // type specified in a non-var assignment
1987 semsg(_(e_trailing_characters_str), p);
1988 else
1989 semsg(_(e_variable_already_declared_str), lhs->lhs_name);
1990 return FAIL;
1991 }
1992 }
1993 else if ((lhs->lhs_classmember_idx = cctx_class_member_idx(
1994 cctx, var_start, lhs->lhs_varlen, &defcl)) >= 0)
1995 {
1996 if (compile_lhs_class_variable(cctx, lhs, defcl, is_decl)
1997 == FAIL)
1998 return FAIL;
1999 }
2000 else
2001 {
2002 if (compile_lhs_script_var(cctx, lhs, var_start, var_end,
2003 is_decl) == FAIL)
2004 return FAIL;
2005 }
2006 }
2007
2008 if (declare_error)
2009 {
2010 vim9_declare_error(lhs->lhs_name);
2011 return FAIL;
2012 }
2013
2014 return OK;
2015}
2016
2017/*
2018 * When compiling a LHS variable name, for a class or an object, set the LHS
2019 * member type.
2020 */
2021 static int
2022compile_lhs_set_oc_member_type(
2023 cctx_T *cctx,
2024 lhs_T *lhs,
2025 char_u *var_start)
2026{
2027 class_T *cl = lhs->lhs_type->tt_class;
2028 int is_object = lhs->lhs_type->tt_type == VAR_OBJECT;
2029 char_u *name = var_start + lhs->lhs_varlen + 1;
2030 size_t namelen = lhs->lhs_end - var_start - lhs->lhs_varlen - 1;
2031
2032 ocmember_T *m = member_lookup(cl, lhs->lhs_type->tt_type,
2033 name, namelen, &lhs->lhs_member_idx);
2034 if (m == NULL)
2035 {
2036 member_not_found_msg(cl, lhs->lhs_type->tt_type, name, namelen);
2037 return FAIL;
2038 }
2039
2040 if (IS_ENUM(cl))
2041 {
2042 if (!inside_class(cctx, cl))
2043 {
2044 semsg(_(e_enumvalue_str_cannot_be_modified),
2045 cl->class_name, m->ocm_name);
Bram Moolenaar752fc692021-01-04 21:57:11 +01002046 return FAIL;
2047 }
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002048 if (lhs->lhs_type->tt_type == VAR_OBJECT &&
2049 lhs->lhs_member_idx < 2)
2050 {
2051 char *msg = lhs->lhs_member_idx == 0 ?
2052 e_enum_str_name_cannot_be_modified :
2053 e_enum_str_ordinal_cannot_be_modified;
2054 semsg(_(msg), cl->class_name);
2055 return FAIL;
2056 }
2057 }
2058
2059 // If it is private member variable, then accessing it outside the
2060 // class is not allowed.
2061 // If it is a read only class variable, then it can be modified
2062 // only inside the class where it is defined.
2063 if ((m->ocm_access != VIM_ACCESS_ALL) &&
2064 ((is_object && !inside_class(cctx, cl))
2065 || (!is_object && cctx->ctx_ufunc->uf_class != cl)))
2066 {
2067 char *msg = (m->ocm_access == VIM_ACCESS_PRIVATE)
2068 ? e_cannot_access_protected_variable_str
2069 : e_variable_is_not_writable_str;
2070 emsg_var_cl_define(msg, m->ocm_name, 0, cl);
2071 return FAIL;
2072 }
2073
2074 if (!IS_CONSTRUCTOR_METHOD(cctx->ctx_ufunc)
2075 && oc_var_check_ro(cl, m))
2076 return FAIL;
2077
2078 lhs->lhs_member_type = m->ocm_type;
2079
2080 return OK;
2081}
2082
2083/*
2084 * When compiling a LHS variable, set the LHS variable type.
2085 */
2086 static int
2087compile_lhs_set_type(cctx_T *cctx, lhs_T *lhs, char_u *var_end, int is_decl)
2088{
2089 if (is_decl && *skipwhite(var_end) == ':')
2090 {
2091 char_u *p;
2092
2093 // parse optional type: "let var: type = expr"
2094 if (VIM_ISWHITE(*var_end))
2095 {
2096 semsg(_(e_no_white_space_allowed_before_colon_str), var_end);
2097 return FAIL;
2098 }
2099
2100 if (!VIM_ISWHITE(var_end[1]))
2101 {
2102 semsg(_(e_white_space_required_after_str_str), ":", var_end);
2103 return FAIL;
2104 }
2105
2106 p = skipwhite(var_end + 1);
2107 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
2108 if (lhs->lhs_type == NULL)
2109 return FAIL;
2110
2111 lhs->lhs_has_type = TRUE;
2112 lhs->lhs_end = p;
2113 }
2114 else if (lhs->lhs_lvar != NULL)
2115 lhs->lhs_type = lhs->lhs_lvar->lv_type;
2116
2117 return OK;
2118}
2119
2120/*
2121 * Returns TRUE if "lhs" is a concatenable string.
2122 */
2123 static int
2124lhs_concatenable(lhs_T *lhs)
2125{
2126 return lhs->lhs_dest == dest_global
2127 || lhs->lhs_has_index
2128 || lhs->lhs_type->tt_type == VAR_STRING
2129 || lhs->lhs_type->tt_type == VAR_ANY;
2130}
2131
2132/*
2133 * Create a new local variable when compiling a LHS variable.
2134 */
2135 static int
2136compile_lhs_new_local_var(
2137 cctx_T *cctx,
2138 lhs_T *lhs,
2139 char_u *var_start,
2140 int cmdidx,
2141 int oplen,
2142 int is_decl,
2143 int has_cmd,
2144 int heredoc)
2145{
2146 if (oplen > 1 && !heredoc)
2147 {
2148 // +=, /=, etc. require an existing variable
2149 semsg(_(e_cannot_use_operator_on_new_variable_str), lhs->lhs_name);
2150 return FAIL;
2151 }
2152
2153 if (!is_decl || (lhs->lhs_has_index && !has_cmd
2154 && cctx->ctx_skip != SKIP_YES))
2155 {
2156 semsg(_(e_unknown_variable_str), lhs->lhs_name);
2157 return FAIL;
2158 }
2159
2160 // Check the name is valid for a funcref.
2161 if (lhs->lhs_type->tt_type == VAR_FUNC
2162 || lhs->lhs_type->tt_type == VAR_PARTIAL)
2163 {
2164 if (var_wrong_func_name(lhs->lhs_name, TRUE))
2165 return FAIL;
2166 }
2167
2168 // New local variable.
2169 int assign;
2170 switch (cmdidx)
2171 {
2172 case CMD_final:
2173 assign = ASSIGN_FINAL; break;
2174 case CMD_const:
2175 assign = ASSIGN_CONST; break;
2176 default:
2177 assign = ASSIGN_VAR; break;
2178 }
2179
2180 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen, assign,
2181 lhs->lhs_type);
2182 if (lhs->lhs_lvar == NULL)
2183 return FAIL;
2184
2185 lhs->lhs_new_local = TRUE;
2186
2187 return OK;
2188}
2189
2190/*
2191 * When compiling a LHS variable name, set the LHS member type.
2192 */
2193 static int
2194compile_lhs_set_member_type(
2195 cctx_T *cctx,
2196 lhs_T *lhs,
2197 char_u *var_start,
2198 int is_decl,
2199 int has_cmd)
2200{
2201 lhs->lhs_member_type = lhs->lhs_type;
2202
2203 if (!lhs->lhs_has_index)
2204 return OK;
2205
2206 char_u *after = var_start + lhs->lhs_varlen;
2207 char_u *p;
2208
2209 // Something follows after the variable: "var[idx]" or "var.key".
2210 if (is_decl && cctx->ctx_skip != SKIP_YES)
2211 {
2212 if (has_cmd)
2213 emsg(_(e_cannot_use_index_when_declaring_variable));
2214 else
2215 semsg(_(e_unknown_variable_str), lhs->lhs_name);
2216 return FAIL;
2217 }
2218
2219 // Now: var_start[lhs->lhs_varlen] is '[' or '.'
2220 // Only the last index is used below, if there are others
2221 // before it generate code for the expression. Thus for
2222 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
2223 for (;;)
2224 {
2225 p = skip_index(after);
2226 if (*p != '[' && *p != '.')
2227 {
2228 lhs->lhs_varlen_total = p - var_start;
2229 break;
2230 }
2231 after = p;
2232 }
2233 if (after > var_start + lhs->lhs_varlen)
2234 {
2235 lhs->lhs_varlen = after - var_start;
2236 lhs->lhs_dest = dest_expr;
2237 // We don't know the type before evaluating the expression,
2238 // use "any" until then.
2239 lhs->lhs_type = &t_any;
2240 }
2241
2242 int use_class = lhs->lhs_type != NULL
2243 && (lhs->lhs_type->tt_type == VAR_CLASS
2244 || lhs->lhs_type->tt_type == VAR_OBJECT);
2245
2246 if (lhs->lhs_type == NULL
2247 || (use_class ? lhs->lhs_type->tt_class == NULL
2248 : lhs->lhs_type->tt_member == NULL))
2249 {
2250 lhs->lhs_member_type = &t_any;
2251 }
2252 else if (use_class)
2253 {
2254 // for an object or class member get the type of the member
2255 if (compile_lhs_set_oc_member_type(cctx, lhs, var_start) == FAIL)
2256 return FAIL;
2257 }
2258 else
2259 lhs->lhs_member_type = lhs->lhs_type->tt_member;
2260
2261 return OK;
2262}
2263
2264/*
2265 * Figure out the LHS type and other properties for an assignment or one item
2266 * of ":unlet" with an index.
2267 * Returns OK or FAIL.
2268 */
2269 int
2270compile_lhs(
2271 char_u *var_start,
2272 lhs_T *lhs,
2273 cmdidx_T cmdidx,
2274 int heredoc,
2275 int has_cmd, // "var" before "var_start"
2276 int oplen,
2277 cctx_T *cctx)
2278{
Yegappan Lakshmanan59c88802024-12-19 20:00:31 +01002279 char_u *var_end = NULL;
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002280 int is_decl = is_decl_command(cmdidx);
2281
2282 if (lhs_init(lhs, var_start, is_decl, heredoc, &var_end) == FAIL)
2283 return FAIL;
2284
2285 if (cctx->ctx_skip != SKIP_YES)
2286 {
2287 // compile the LHS destination
2288 if (compile_lhs_var_dest(cctx, lhs, cmdidx, var_start, var_end,
2289 is_decl) == FAIL)
2290 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002291 }
2292
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00002293 // handle "a:name" as a name, not index "name" in "a"
Bram Moolenaar752fc692021-01-04 21:57:11 +01002294 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
2295 var_end = lhs->lhs_dest_end;
2296
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002297 if (lhs->lhs_dest != dest_option && lhs->lhs_dest != dest_func_option)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002298 {
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002299 // set the LHS variable type
2300 if (compile_lhs_set_type(cctx, lhs, var_end, is_decl) == FAIL)
2301 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002302 }
2303
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002304 if (oplen == 3 && !heredoc && !lhs_concatenable(lhs))
Bram Moolenaar752fc692021-01-04 21:57:11 +01002305 {
2306 emsg(_(e_can_only_concatenate_to_string));
2307 return FAIL;
2308 }
2309
2310 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002311 && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002312 {
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002313 if (compile_lhs_new_local_var(cctx, lhs, var_start, cmdidx, oplen,
2314 is_decl, has_cmd, heredoc) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002315 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002316 }
2317
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002318 if (compile_lhs_set_member_type(cctx, lhs, var_start, is_decl, has_cmd)
2319 == FAIL)
2320 return FAIL;
Bram Moolenaarc750d912021-11-29 22:02:12 +00002321
Bram Moolenaar752fc692021-01-04 21:57:11 +01002322 return OK;
2323}
2324
2325/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002326 * Figure out the LHS and check a few errors.
2327 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002328 int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002329compile_assign_lhs(
Bram Moolenaar97f8c102022-04-02 19:43:57 +01002330 char_u *var_start,
2331 lhs_T *lhs,
2332 cmdidx_T cmdidx,
2333 int is_decl,
2334 int heredoc,
2335 int has_cmd, // "var" before "var_start"
2336 int oplen,
2337 cctx_T *cctx)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002338{
Bram Moolenaar97f8c102022-04-02 19:43:57 +01002339 if (compile_lhs(var_start, lhs, cmdidx, heredoc, has_cmd, oplen, cctx)
2340 == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002341 return FAIL;
2342
2343 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
2344 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002345 semsg(_(e_cannot_assign_to_argument_str), lhs->lhs_name);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002346 return FAIL;
2347 }
2348 if (!is_decl && lhs->lhs_lvar != NULL
Bram Moolenaar6586a012022-09-30 11:04:50 +01002349 && lhs->lhs_lvar->lv_const != ASSIGN_VAR
2350 && !lhs->lhs_has_index)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002351 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002352 semsg(_(e_cannot_assign_to_constant_str), lhs->lhs_name);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002353 return FAIL;
2354 }
2355 return OK;
2356}
2357
2358/*
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002359 * Return TRUE if "lhs" has a range index: "[expr : expr]".
2360 */
2361 static int
2362has_list_index(char_u *idx_start, cctx_T *cctx)
2363{
2364 char_u *p = idx_start;
2365 int save_skip;
2366
2367 if (*p != '[')
2368 return FALSE;
2369
2370 p = skipwhite(p + 1);
2371 if (*p == ':')
2372 return TRUE;
2373
2374 save_skip = cctx->ctx_skip;
2375 cctx->ctx_skip = SKIP_YES;
2376 (void)compile_expr0(&p, cctx);
2377 cctx->ctx_skip = save_skip;
2378 return *skipwhite(p) == ':';
2379}
2380
2381/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002382 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
2383 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01002384 */
2385 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02002386compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01002387 char_u *var_start,
2388 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002389 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01002390 cctx_T *cctx)
2391{
Bram Moolenaar752fc692021-01-04 21:57:11 +01002392 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002393 char_u *p;
2394 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02002395 int need_white_before = TRUE;
2396 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002397
Bram Moolenaar752fc692021-01-04 21:57:11 +01002398 p = var_start + varlen;
2399 if (*p == '[')
2400 {
2401 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02002402 if (*p == ':')
2403 {
2404 // empty first index, push zero
2405 r = generate_PUSHNR(cctx, 0);
2406 need_white_before = FALSE;
2407 }
2408 else
2409 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002410
2411 if (r == OK && *skipwhite(p) == ':')
2412 {
2413 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02002414 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02002415 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002416 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02002417 empty_second = *skipwhite(p + 1) == ']';
2418 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
2419 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002420 {
2421 semsg(_(e_white_space_required_before_and_after_str_at_str),
2422 ":", p);
2423 return FAIL;
2424 }
2425 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02002426 if (*p == ']')
2427 // empty second index, push "none"
2428 r = generate_PUSHSPEC(cctx, VVAL_NONE);
2429 else
2430 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002431 }
2432
Bram Moolenaar752fc692021-01-04 21:57:11 +01002433 if (r == OK && *skipwhite(p) != ']')
2434 {
2435 // this should not happen
Bram Moolenaare1242042021-12-16 20:56:57 +00002436 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar752fc692021-01-04 21:57:11 +01002437 r = FAIL;
2438 }
2439 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002440 else if (lhs->lhs_member_idx >= 0)
2441 {
2442 // object member index
2443 r = generate_PUSHNR(cctx, lhs->lhs_member_idx);
2444 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002445 else // if (*p == '.')
2446 {
2447 char_u *key_end = to_name_end(p + 1, TRUE);
2448 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
2449
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02002450 r = generate_PUSHS(cctx, &key);
Bram Moolenaar752fc692021-01-04 21:57:11 +01002451 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02002452 return r;
2453}
2454
2455/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002456 * For a LHS with an index, load the variable to be indexed.
2457 */
2458 static int
2459compile_load_lhs(
2460 lhs_T *lhs,
2461 char_u *var_start,
2462 type_T *rhs_type,
2463 cctx_T *cctx)
2464{
2465 if (lhs->lhs_dest == dest_expr)
2466 {
2467 size_t varlen = lhs->lhs_varlen;
2468 int c = var_start[varlen];
Bram Moolenaare97976b2021-08-01 13:17:17 +02002469 int lines_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaare97976b2021-08-01 13:17:17 +02002470 int res;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002471
Bram Moolenaare97976b2021-08-01 13:17:17 +02002472 // Evaluate "ll[expr]" of "ll[expr][idx]". End the line with a NUL and
2473 // limit the lines array length to avoid skipping to a following line.
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002474 var_start[varlen] = NUL;
Bram Moolenaare97976b2021-08-01 13:17:17 +02002475 cctx->ctx_ufunc->uf_lines.ga_len = cctx->ctx_lnum + 1;
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002476 char_u *p = var_start;
Bram Moolenaare97976b2021-08-01 13:17:17 +02002477 res = compile_expr0(&p, cctx);
2478 var_start[varlen] = c;
2479 cctx->ctx_ufunc->uf_lines.ga_len = lines_len;
2480 if (res == FAIL || p != var_start + varlen)
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002481 {
2482 // this should not happen
Bram Moolenaare97976b2021-08-01 13:17:17 +02002483 if (res != FAIL)
Bram Moolenaare1242042021-12-16 20:56:57 +00002484 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002485 return FAIL;
2486 }
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002487
Bram Moolenaar078a4612022-01-04 15:17:03 +00002488 lhs->lhs_type = cctx->ctx_type_stack.ga_len == 0 ? &t_void
2489 : get_type_on_stack(cctx, 0);
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02002490
Yegappan Lakshmanan54d7f182025-02-10 21:35:07 +01002491 if (lhs->lhs_type->tt_type == VAR_CLASS
Yegappan Lakshmanande8f8f72025-04-01 20:43:36 +02002492 || (lhs->lhs_type->tt_type == VAR_OBJECT
2493 && lhs->lhs_type != &t_object_any))
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02002494 {
Yegappan Lakshmanan54d7f182025-02-10 21:35:07 +01002495 // Check whether the class or object variable is modifiable
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02002496 if (!lhs_class_member_modifiable(lhs, var_start, cctx))
2497 return FAIL;
2498 }
2499
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002500 // Now we can properly check the type. The variable is indexed, thus
2501 // we need the member type. For a class or object we don't know the
2502 // type yet, it depends on what member is used.
Yegappan Lakshmananc229a6a2023-10-26 23:05:07 +02002503 // The top item in the stack is the Dict, followed by the key and then
2504 // the type of the value.
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002505 vartype_T vartype = lhs->lhs_type->tt_type;
2506 type_T *member_type = lhs->lhs_type->tt_member;
2507 if (rhs_type != NULL && member_type != NULL
2508 && vartype != VAR_OBJECT && vartype != VAR_CLASS
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002509 && rhs_type != &t_void
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002510 && need_type(rhs_type, member_type, FALSE,
Yegappan Lakshmananc229a6a2023-10-26 23:05:07 +02002511 -3, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002512 return FAIL;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002513
2514 return OK;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002515 }
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002516
Yegappan Lakshmanande8f8f72025-04-01 20:43:36 +02002517 return generate_loadvar(cctx, lhs);
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002518}
2519
2520/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02002521 * Produce code for loading "lhs" and also take care of an index.
2522 * Return OK/FAIL.
2523 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002524 int
Bram Moolenaara369c3d2021-04-21 16:00:10 +02002525compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
2526{
Bram Moolenaar4cae8452023-01-15 15:51:48 +00002527 if (lhs->lhs_type->tt_type == VAR_OBJECT)
2528 {
Bram Moolenaar22363c62023-04-24 17:15:25 +01002529 // "this.value": load "this" object and get the value at index for an
2530 // object or class member get the type of the member.
2531 // Also for "obj.value".
Yegappan Lakshmananff6f0d52023-12-21 16:46:18 +01002532 char_u *dot = vim_strchr(var_start, '.');
2533 if (dot == NULL)
2534 {
2535 semsg(_(e_missing_dot_after_object_str), lhs->lhs_name);
2536 return FAIL;
2537 }
Bram Moolenaar22363c62023-04-24 17:15:25 +01002538
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +02002539 class_T *cl = lhs->lhs_type->tt_class;
Yegappan Lakshmanan1ea42882023-10-11 21:43:52 +02002540 type_T *type = oc_member_type(cl, TRUE, dot + 1,
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02002541 lhs->lhs_end, &lhs->lhs_member_idx);
Bram Moolenaar4cae8452023-01-15 15:51:48 +00002542 if (lhs->lhs_member_idx < 0)
2543 return FAIL;
2544
Bram Moolenaar22363c62023-04-24 17:15:25 +01002545 if (dot - var_start == 4 && STRNCMP(var_start, "this", 4) == 0)
2546 {
2547 // load "this"
Yegappan Lakshmanand990bf02024-03-22 19:56:17 +01002548 lvar_T *lvar = lhs->lhs_lvar;
2549 int rc;
2550
2551 if (lvar->lv_from_outer > 0)
2552 rc = generate_LOADOUTER(cctx, lvar->lv_idx,
2553 lvar->lv_from_outer, lvar->lv_loop_depth,
2554 lvar->lv_loop_idx, type);
2555 else
2556 rc = generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
2557
2558 if (rc == FAIL)
Bram Moolenaar22363c62023-04-24 17:15:25 +01002559 return FAIL;
2560 }
2561 else
2562 {
2563 // load object variable or argument
2564 if (compile_load_lhs(lhs, var_start, lhs->lhs_type, cctx) == FAIL)
2565 return FAIL;
2566 }
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01002567 if (IS_INTERFACE(cl))
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02002568 return generate_GET_ITF_MEMBER(cctx, cl, lhs->lhs_member_idx, type);
2569 return generate_GET_OBJ_MEMBER(cctx, lhs->lhs_member_idx, type);
Bram Moolenaar4cae8452023-01-15 15:51:48 +00002570 }
Yegappan Lakshmanan1ea42882023-10-11 21:43:52 +02002571 else if (lhs->lhs_type->tt_type == VAR_CLASS)
2572 {
2573 // "<classname>.value": load class variable "classname.value"
Yegappan Lakshmananff6f0d52023-12-21 16:46:18 +01002574 char_u *dot = vim_strchr(var_start, '.');
2575 if (dot == NULL)
2576 {
2577 check_type_is_value(lhs->lhs_type);
2578 return FAIL;
2579 }
Yegappan Lakshmanan1ea42882023-10-11 21:43:52 +02002580
2581 class_T *cl = lhs->lhs_type->tt_class;
2582 ocmember_T *m = class_member_lookup(cl, dot + 1,
2583 lhs->lhs_end - dot - 1,
2584 &lhs->lhs_member_idx);
2585 if (m == NULL)
2586 return FAIL;
2587
2588 return generate_CLASSMEMBER(cctx, TRUE, cl, lhs->lhs_member_idx);
2589 }
Bram Moolenaar4cae8452023-01-15 15:51:48 +00002590
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002591 if (compile_load_lhs(lhs, var_start, NULL, cctx) == FAIL)
2592 return FAIL;
Bram Moolenaara369c3d2021-04-21 16:00:10 +02002593
2594 if (lhs->lhs_has_index)
2595 {
2596 int range = FALSE;
2597
2598 // Get member from list or dict. First compile the
2599 // index value.
2600 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
2601 return FAIL;
2602 if (range)
2603 {
2604 semsg(_(e_cannot_use_range_with_assignment_operator_str),
2605 var_start);
2606 return FAIL;
2607 }
2608
2609 // Get the member.
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002610 if (compile_member(FALSE, NULL, cctx) == FAIL)
Bram Moolenaara369c3d2021-04-21 16:00:10 +02002611 return FAIL;
2612 }
2613 return OK;
2614}
2615
2616/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002617 * Assignment to a list or dict member, or ":unlet" for the item, using the
2618 * information in "lhs".
2619 * Returns OK or FAIL.
2620 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002621 int
Bram Moolenaare42939a2021-04-05 17:11:17 +02002622compile_assign_unlet(
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002623 char_u *var_start,
2624 lhs_T *lhs,
2625 int is_assign,
2626 type_T *rhs_type,
2627 cctx_T *cctx)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002628{
Bram Moolenaare42939a2021-04-05 17:11:17 +02002629 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002630 int range = FALSE;
2631
Bram Moolenaar68452172021-04-12 21:21:02 +02002632 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002633 return FAIL;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002634 if (is_assign && range
2635 && lhs->lhs_type->tt_type != VAR_LIST
2636 && lhs->lhs_type != &t_blob
2637 && lhs->lhs_type != &t_any)
Bram Moolenaar68452172021-04-12 21:21:02 +02002638 {
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002639 if (lhs->lhs_type->tt_type == VAR_TUPLE)
2640 emsg(_(e_cannot_slice_tuple));
2641 else
2642 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
Bram Moolenaar68452172021-04-12 21:21:02 +02002643 return FAIL;
2644 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002645
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002646 if (lhs->lhs_type == NULL || lhs->lhs_type == &t_any)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002647 {
2648 // Index on variable of unknown type: check at runtime.
2649 dest_type = VAR_ANY;
2650 }
2651 else
2652 {
2653 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002654 if (dest_type == VAR_DICT && range)
2655 {
zeertzjq276410e2023-05-07 21:59:33 +01002656 emsg(_(e_cannot_use_range_with_dictionary));
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002657 return FAIL;
2658 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002659 if (dest_type == VAR_DICT
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02002660 && may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002661 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02002662 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002663 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002664 type_T *type;
2665
2666 if (range)
2667 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00002668 type = get_type_on_stack(cctx, 1);
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002669 if (need_type(type, &t_number, FALSE,
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002670 -2, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002671 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02002672 }
Bram Moolenaar078a4612022-01-04 15:17:03 +00002673 type = get_type_on_stack(cctx, 0);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00002674 if ((dest_type != VAR_BLOB && type->tt_type != VAR_SPECIAL)
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002675 && need_type(type, &t_number, FALSE,
Bram Moolenaar51e93322021-04-17 20:44:56 +02002676 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002677 return FAIL;
2678 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002679 }
2680
Bram Moolenaar4875d6a2022-08-17 15:55:51 +01002681 if (cctx->ctx_skip == SKIP_YES)
2682 return OK;
2683
Bram Moolenaar22363c62023-04-24 17:15:25 +01002684 // Load the dict, list or object. On the stack we then have:
Bram Moolenaar752fc692021-01-04 21:57:11 +01002685 // - value (for assignment, not for :unlet)
2686 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002687 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01002688 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002689 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
2690 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002691
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002692 if (dest_type == VAR_LIST
2693 || dest_type == VAR_DICT
2694 || dest_type == VAR_BLOB
2695 || dest_type == VAR_CLASS
2696 || dest_type == VAR_OBJECT
2697 || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002698 {
2699 if (is_assign)
2700 {
Bram Moolenaar68452172021-04-12 21:21:02 +02002701 if (range)
2702 {
2703 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
2704 return FAIL;
2705 }
2706 else
2707 {
2708 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01002709
Bram Moolenaar68452172021-04-12 21:21:02 +02002710 if (isn == NULL)
2711 return FAIL;
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002712 isn->isn_arg.storeindex.si_vartype = dest_type;
2713 isn->isn_arg.storeindex.si_class = NULL;
2714
2715 if (dest_type == VAR_OBJECT)
2716 {
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +00002717 class_T *cl = lhs->lhs_type->tt_class;
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002718
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01002719 if (IS_INTERFACE(cl))
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002720 {
2721 // "this.value": load "this" object and get the value
2722 // at index for an object or class member get the type
2723 // of the member
2724 isn->isn_arg.storeindex.si_class = cl;
2725 ++cl->class_refcount;
2726 }
2727 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002728 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002729 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002730 else if (range)
2731 {
2732 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
2733 return FAIL;
2734 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002735 else
2736 {
2737 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
2738 return FAIL;
2739 }
2740 }
2741 else
2742 {
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002743 if (dest_type == VAR_TUPLE)
2744 emsg(_(e_tuple_is_immutable));
2745 else
2746 emsg(_(e_indexable_type_required));
Bram Moolenaar752fc692021-01-04 21:57:11 +01002747 return FAIL;
2748 }
2749
2750 return OK;
2751}
2752
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002753/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002754 * Generate an instruction to push the default value for "vartype".
2755 * if "dest_local" is TRUE then for some types no instruction is generated.
2756 * "skip_store" is set to TRUE if no PUSH instruction is generated.
2757 * Returns OK or FAIL.
2758 */
2759 static int
2760push_default_value(
2761 cctx_T *cctx,
2762 vartype_T vartype,
2763 int dest_is_local,
2764 int *skip_store)
2765{
2766 int r = OK;
2767
2768 switch (vartype)
2769 {
2770 case VAR_BOOL:
2771 r = generate_PUSHBOOL(cctx, VVAL_FALSE);
2772 break;
2773 case VAR_FLOAT:
2774 r = generate_PUSHF(cctx, 0.0);
2775 break;
2776 case VAR_STRING:
2777 r = generate_PUSHS(cctx, NULL);
2778 break;
2779 case VAR_BLOB:
2780 r = generate_PUSHBLOB(cctx, blob_alloc());
2781 break;
2782 case VAR_FUNC:
2783 r = generate_PUSHFUNC(cctx, NULL, &t_func_void, TRUE);
2784 break;
2785 case VAR_LIST:
2786 r = generate_NEWLIST(cctx, 0, FALSE);
2787 break;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002788 case VAR_TUPLE:
2789 r = generate_NEWTUPLE(cctx, 0, FALSE);
2790 break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002791 case VAR_DICT:
2792 r = generate_NEWDICT(cctx, 0, FALSE);
2793 break;
2794 case VAR_JOB:
2795 r = generate_PUSHJOB(cctx);
2796 break;
2797 case VAR_CHANNEL:
2798 r = generate_PUSHCHANNEL(cctx);
2799 break;
Ernie Rael5c018be2023-08-27 18:40:26 +02002800 case VAR_OBJECT:
2801 r = generate_PUSHOBJ(cctx);
2802 break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002803 case VAR_NUMBER:
2804 case VAR_UNKNOWN:
2805 case VAR_ANY:
2806 case VAR_PARTIAL:
2807 case VAR_VOID:
2808 case VAR_INSTR:
2809 case VAR_CLASS:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002810 case VAR_TYPEALIAS:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002811 case VAR_SPECIAL: // cannot happen
2812 // This is skipped for local variables, they are always
2813 // initialized to zero. But in a "for" or "while" loop
2814 // the value may have been changed.
2815 if (dest_is_local && !inside_loop_scope(cctx))
2816 *skip_store = TRUE;
2817 else
2818 r = generate_PUSHNR(cctx, 0);
2819 break;
2820 }
2821 return r;
2822}
2823
2824/*
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002825 * Compile assignment context. Used when compiling an assignment statement.
2826 */
2827typedef struct cac_S cac_T;
2828struct cac_S
2829{
2830 cmdidx_T cac_cmdidx; // assignment command
2831 char_u *cac_nextc; // next character to parse
2832 lhs_T cac_lhs; // lhs of the assignment
2833 type_T *cac_rhs_type; // rhs type of an assignment
2834 char_u *cac_op; // assignment operator
2835 int cac_oplen; // assignment operator length
2836 char_u *cac_var_start; // start of the variable names
2837 char_u *cac_var_end; // end of the variable names
2838 int cac_var_count; // number of variables in assignment
2839 int cac_var_idx; // variable index in a list
2840 int cac_semicolon; // semicolon in [var1, var2; var3]
2841 garray_T *cac_instr;
2842 int cac_instr_count;
2843 int cac_incdec;
2844 int cac_did_generate_slice;
2845 int cac_is_decl;
2846 int cac_is_const;
2847 int cac_start_lnum;
2848 type_T *cac_inferred_type;
2849 int cac_skip_store;
2850};
2851
2852/*
2853 * Initialize the compile assignment context.
2854 */
2855 static void
2856compile_assign_context_init(cac_T *cac, cctx_T *cctx, int cmdidx, char_u *arg)
2857{
2858 CLEAR_FIELD(*cac);
2859 cac->cac_cmdidx = cmdidx;
2860 cac->cac_instr = &cctx->ctx_instr;
2861 cac->cac_rhs_type = &t_any;
2862 cac->cac_is_decl = is_decl_command(cmdidx);
2863 cac->cac_start_lnum = SOURCING_LNUM;
2864 cac->cac_instr_count = -1;
2865 cac->cac_var_end = arg;
2866}
2867
2868/*
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01002869 * Compile an object member variable assignment in the arguments passed to a
2870 * class new() method.
2871 *
2872 * Instruction format:
2873 *
2874 * ifargisset <n> this.<varname> = <value>
2875 *
2876 * where <n> is the index of the default argument.
2877 *
2878 * Generates the ISN_JUMP_IF_ARG_NOT_SET instruction to skip the assignment if
2879 * the value is passed as an argument to the new() method call.
2880 *
2881 * Returns OK on success.
2882 */
2883 static int
2884compile_assign_obj_new_arg(char_u **argp, cctx_T *cctx)
2885{
2886 char_u *arg = *argp;
2887
2888 arg += 11; // skip "ifargisset"
2889 int def_arg_idx = getdigits(&arg);
2890 arg = skipwhite(arg);
2891
2892 // Use a JUMP_IF_ARG_NOT_SET instruction to skip if the value was not
2893 // given and the default value is "v:none".
2894 int stack_offset = STACK_FRAME_SIZE +
2895 (cctx->ctx_ufunc->uf_va_name != NULL ? 1 : 0);
2896 int def_arg_count = cctx->ctx_ufunc->uf_def_args.ga_len;
2897 int arg_offset = def_arg_idx - def_arg_count - stack_offset;
2898
2899 if (generate_JUMP_IF_ARG(cctx, ISN_JUMP_IF_ARG_NOT_SET,
2900 arg_offset) == FAIL)
2901 return FAIL;
2902
2903 *argp = arg;
2904 return OK;
2905}
2906
2907/*
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002908 * Translate the increment (++) and decrement (--) operators to the
2909 * corresponding compound operators (+= or -=).
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01002910 *
2911 * Returns OK on success and FAIL on syntax error.
2912 */
2913 static int
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002914translate_incdec_op(exarg_T *eap, cac_T *cac)
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01002915{
2916 if (VIM_ISWHITE(eap->cmd[2]))
2917 {
2918 semsg(_(e_no_white_space_allowed_after_str_str),
2919 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
2920 return FAIL;
2921 }
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002922 cac->cac_op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
2923 cac->cac_oplen = 2;
2924 cac->cac_incdec = TRUE;
2925
2926 return OK;
2927}
2928
2929/*
2930 * Process the operator in an assignment statement.
2931 */
2932 static int
2933compile_assign_process_operator(
2934 exarg_T *eap,
2935 char_u *arg,
2936 cac_T *cac,
2937 int *heredoc,
2938 char_u **retstr)
2939{
2940 *retstr = NULL;
2941
2942 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002943 // Change an unary operator to a compound operator
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01002944 return translate_incdec_op(eap, cac);
2945
2946 char_u *sp = cac->cac_nextc;
2947 cac->cac_nextc = skipwhite(cac->cac_nextc);
2948 cac->cac_op = cac->cac_nextc;
2949 cac->cac_oplen = assignment_len(cac->cac_nextc, heredoc);
2950
2951 if (cac->cac_var_count > 0 && cac->cac_oplen == 0)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002952 {
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01002953 // can be something like "[1, 2]->func()"
2954 *retstr = arg;
2955 return FAIL;
2956 }
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002957
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01002958 // need white space before and after the operator
2959 if (cac->cac_oplen > 0 && (!VIM_ISWHITE(*sp)
2960 || !IS_WHITE_OR_NUL(cac->cac_op[cac->cac_oplen])))
2961 {
2962 error_white_both(cac->cac_op, cac->cac_oplen);
2963 return FAIL;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002964 }
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01002965
2966 return OK;
2967}
2968
2969/*
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01002970 * Find the start of an assignment statement.
2971 */
2972 static char_u *
2973compile_assign_compute_start(char_u *arg, int var_count)
2974{
2975 if (var_count > 0)
2976 // [var1, var2] = [val1, val2]
2977 // skip over the "["
2978 return skipwhite(arg + 1);
2979
2980 return arg;
2981}
2982
2983/*
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01002984 * Parse a heredoc assignment starting at "p". Returns a pointer to the
2985 * beginning of the heredoc content.
2986 */
2987 static char_u *
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002988parse_heredoc_assignment(exarg_T *eap, cctx_T *cctx, cac_T *cac)
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01002989{
2990 // [let] varname =<< [trim] {end}
2991 eap->ea_getline = exarg_getline;
2992 eap->cookie = cctx;
2993
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002994 list_T *l = heredoc_get(eap, cac->cac_nextc + 3, FALSE, TRUE);
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01002995 if (l == NULL)
2996 return NULL;
2997
2998 list_free(l);
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002999 cac->cac_nextc += STRLEN(cac->cac_nextc);
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003000
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003001 return cac->cac_nextc;
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003002}
3003
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003004/*
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003005 * Check the type of a RHS expression in a list assignment statement.
3006 * The RHS expression is already compiled. So the type is on the stack.
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003007 */
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003008 static int
3009compile_assign_list_check_rhs_type(cctx_T *cctx, cac_T *cac)
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003010{
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003011 type_T *stacktype;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003012
3013 stacktype = cctx->ctx_type_stack.ga_len == 0 ? &t_void
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003014 : get_type_on_stack(cctx, 0);
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003015 if (stacktype->tt_type == VAR_VOID)
3016 {
3017 emsg(_(e_cannot_use_void_value));
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003018 return FAIL;
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003019 }
3020
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01003021 if (stacktype->tt_type != VAR_LIST && stacktype->tt_type != VAR_TUPLE
3022 && stacktype->tt_type != VAR_ANY)
3023 {
3024 emsg(_(e_list_or_tuple_required));
3025 return FAIL;
3026 }
3027
3028 if (need_type(stacktype,
3029 stacktype->tt_type == VAR_TUPLE ? &t_tuple_any : &t_list_any,
3030 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003031 return FAIL;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003032
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01003033 if (stacktype->tt_type == VAR_TUPLE)
3034 {
3035 if (stacktype->tt_argcount != 1)
3036 cac->cac_rhs_type = &t_any;
3037 else
3038 {
3039 if (stacktype->tt_flags & TTFLAG_VARARGS)
3040 cac->cac_rhs_type = stacktype->tt_args[0]->tt_member;
3041 else
3042 cac->cac_rhs_type = stacktype->tt_args[0];
3043 }
3044 }
3045 else if (stacktype->tt_member != NULL)
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003046 cac->cac_rhs_type = stacktype->tt_member;
3047
3048 return OK;
3049}
3050
3051/*
3052 * In a list assignment statement, if a constant list was used, check the
3053 * length. Returns OK if the length check succeeds. Returns FAIL otherwise.
3054 */
3055 static int
3056compile_assign_list_check_length(cctx_T *cctx, cac_T *cac)
3057{
3058 int needed_list_len;
3059 int did_check = FALSE;
3060
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003061 needed_list_len = cac->cac_semicolon
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003062 ? cac->cac_var_count - 1
3063 : cac->cac_var_count;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003064 if (cac->cac_instr->ga_len > 0)
3065 {
3066 isn_T *isn = ((isn_T *)cac->cac_instr->ga_data) +
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003067 cac->cac_instr->ga_len - 1;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003068
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01003069 if (isn->isn_type == ISN_NEWLIST || isn->isn_type == ISN_NEWTUPLE)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003070 {
3071 did_check = TRUE;
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003072 if (cac->cac_semicolon ?
3073 isn->isn_arg.number < needed_list_len
3074 : isn->isn_arg.number != needed_list_len)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003075 {
3076 semsg(_(e_expected_nr_items_but_got_nr),
3077 needed_list_len, (int)isn->isn_arg.number);
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003078 return FAIL;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003079 }
3080 }
3081 }
3082
3083 if (!did_check)
3084 generate_CHECKLEN(cctx, needed_list_len, cac->cac_semicolon);
3085
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003086 return OK;
3087}
3088
3089/*
3090 * Evaluate the expression for "[var, var] = expr" assignment.
3091 * A line break may follow the assignment operator "=".
3092 */
3093 static char_u *
3094compile_assign_list_expr(cctx_T *cctx, cac_T *cac)
3095{
3096 char_u *whitep;
3097
3098 whitep = cac->cac_op + cac->cac_oplen;
3099
3100 if (may_get_next_line_error(whitep, &cac->cac_nextc, cctx) == FAIL)
3101 return NULL;
3102
3103 // compile RHS expression
3104 if (compile_expr0(&cac->cac_nextc, cctx) == FAIL)
3105 return NULL;
3106
3107 if (cctx->ctx_skip == SKIP_YES)
3108 // no need to parse more when skipping
3109 return cac->cac_nextc;
3110
3111 if (compile_assign_list_check_rhs_type(cctx, cac) == FAIL)
3112 return NULL;
3113
3114 // If a constant list was used we can check the length right here.
3115 if (compile_assign_list_check_length(cctx, cac) == FAIL)
3116 return FAIL;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003117
3118 return cac->cac_nextc;
3119}
3120
3121/*
3122 * Find and return the end of a heredoc or a list of variables assignment
3123 * statement. For a single variable assignment statement, returns the current
3124 * end.
3125 * Returns NULL on failure.
3126 */
3127 static char_u *
3128compile_assign_compute_end(
3129 exarg_T *eap,
3130 cctx_T *cctx,
3131 cac_T *cac,
3132 int heredoc)
3133{
3134 if (heredoc)
3135 {
3136 cac->cac_nextc = parse_heredoc_assignment(eap, cctx, cac);
3137 return cac->cac_nextc;
3138 }
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003139
3140 if (cac->cac_var_count > 0)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003141 {
3142 // for "[var, var] = expr" evaluate the expression. The list of
3143 // variables are processed later.
3144 // A line break may follow the "=".
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003145 cac->cac_nextc = compile_assign_list_expr(cctx, cac);
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003146 return cac->cac_nextc;
3147 }
3148
3149 return cac->cac_var_end;
3150}
3151
3152/*
3153 * For "var = expr" evaluate the expression.
3154 */
3155 static int
3156compile_assign_single_eval_expr(cctx_T *cctx, cac_T *cac)
3157{
3158 int ret = OK;
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003159 char_u *whitep;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003160 lhs_T *lhs = &cac->cac_lhs;
3161
3162 // Compile the expression.
3163 if (cac->cac_incdec)
3164 return generate_PUSHNR(cctx, 1);
3165
3166 // Temporarily hide the new local variable here, it is
3167 // not available to this expression.
3168 if (lhs->lhs_new_local)
3169 --cctx->ctx_locals.ga_len;
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003170 whitep = cac->cac_op + cac->cac_oplen;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003171
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003172 if (may_get_next_line_error(whitep, &cac->cac_nextc, cctx) == FAIL)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003173 {
3174 if (lhs->lhs_new_local)
3175 ++cctx->ctx_locals.ga_len;
3176 return FAIL;
3177 }
3178
3179 ret = compile_expr0_ext(&cac->cac_nextc, cctx, &cac->cac_is_const);
3180 if (lhs->lhs_new_local)
3181 ++cctx->ctx_locals.ga_len;
3182
3183 return ret;
3184}
3185
3186/*
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003187 * When compiling an assignment, set the LHS type to the RHS type.
3188 */
3189 static int
3190compile_assign_set_lhs_type_from_rhs(
3191 cctx_T *cctx,
3192 cac_T *cac,
3193 lhs_T *lhs,
3194 type_T *rhs_type)
3195{
3196 if (rhs_type->tt_type == VAR_VOID)
3197 {
3198 emsg(_(e_cannot_use_void_value));
3199 return FAIL;
3200 }
3201
3202 type_T *type;
3203
3204 // An empty list or dict has a &t_unknown member, for a variable that
3205 // implies &t_any.
3206 if (rhs_type == &t_list_empty)
3207 type = &t_list_any;
3208 else if (rhs_type == &t_dict_empty)
3209 type = &t_dict_any;
3210 else if (rhs_type == &t_unknown)
3211 type = &t_any;
3212 else
3213 {
3214 type = rhs_type;
3215 cac->cac_inferred_type = rhs_type;
3216 }
3217
3218 set_var_type(lhs->lhs_lvar, type, cctx);
3219
3220 return OK;
3221}
3222
3223/*
3224 * Returns TRUE if the "rhs_type" can be assigned to the "lhs" variable.
3225 * Used when compiling an assignment statement.
3226 */
3227 static int
3228compile_assign_valid_rhs_type(
3229 cctx_T *cctx,
3230 cac_T *cac,
3231 lhs_T *lhs,
3232 type_T *rhs_type)
3233{
3234 type_T *use_type = lhs->lhs_lvar->lv_type;
3235 where_T where = WHERE_INIT;
3236
3237 // Without operator check type here, otherwise below.
3238 // Use the line number of the assignment.
3239 SOURCING_LNUM = cac->cac_start_lnum;
3240 if (cac->cac_var_count > 0)
3241 {
3242 where.wt_index = cac->cac_var_idx + 1;
3243 where.wt_kind = WT_VARIABLE;
3244 }
3245
3246 // If assigning to a list or dict member, use the member type.
3247 // Not for "list[:] =".
3248 if (lhs->lhs_has_index &&
3249 !has_list_index(cac->cac_var_start + lhs->lhs_varlen, cctx))
3250 use_type = lhs->lhs_member_type;
3251
3252 if (need_type_where(rhs_type, use_type, FALSE, -1, where, cctx, FALSE,
3253 cac->cac_is_const) == FAIL)
3254 return FALSE;
3255
3256 return TRUE;
3257}
3258
3259/*
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003260 * Compare the LHS type with the RHS type in an assignment.
3261 */
3262 static int
3263compile_assign_check_type(cctx_T *cctx, cac_T *cac)
3264{
3265 lhs_T *lhs = &cac->cac_lhs;
3266 type_T *rhs_type;
3267
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003268 rhs_type = cctx->ctx_type_stack.ga_len == 0
3269 ? &t_void
3270 : get_type_on_stack(cctx, 0);
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003271 cac->cac_rhs_type = rhs_type;
3272
3273 if (check_type_is_value(rhs_type) == FAIL)
3274 return FAIL;
3275
3276 if (lhs->lhs_lvar != NULL && (cac->cac_is_decl || !lhs->lhs_has_type))
3277 {
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003278 if (rhs_type->tt_type == VAR_FUNC
3279 || rhs_type->tt_type == VAR_PARTIAL)
3280 {
3281 // Make sure the variable name can be used as a funcref
3282 if (!lhs->lhs_has_index
3283 && var_wrong_func_name(lhs->lhs_name, TRUE))
3284 return FAIL;
3285 }
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003286
3287 if (lhs->lhs_new_local && !lhs->lhs_has_type)
3288 {
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003289 // The LHS variable doesn't have a type. Set it to the RHS type.
3290 if (compile_assign_set_lhs_type_from_rhs(cctx, cac, lhs, rhs_type)
3291 == FAIL)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003292 return FAIL;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003293 }
3294 else if (*cac->cac_op == '=')
3295 {
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003296 if (!compile_assign_valid_rhs_type(cctx, cac, lhs, rhs_type))
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003297 return FAIL;
3298 }
3299 }
3300 else
3301 {
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003302 // Assigning to a register using @r = "abc"
3303
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003304 type_T *lhs_type = lhs->lhs_member_type;
3305
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003306 // Special case: assigning to @# can use a number or a string.
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003307 // Also: can assign a number to a float.
3308 if ((lhs_type == &t_number_or_string || lhs_type == &t_float)
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003309 && rhs_type->tt_type == VAR_NUMBER)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003310 lhs_type = &t_number;
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003311
3312 if (*cac->cac_nextc != '=')
3313 {
3314 if (need_type(rhs_type, lhs_type, FALSE, -1, 0, cctx, FALSE,
3315 FALSE) == FAIL)
3316 return FAIL;
3317 }
3318 }
3319
3320 return OK;
3321}
3322
3323/*
3324 * Compile the RHS expression in an assignment statement and generate the
3325 * instructions.
3326 */
3327 static int
3328compile_assign_rhs_expr(cctx_T *cctx, cac_T *cac)
3329{
3330 cac->cac_is_const = FALSE;
3331
3332 // for "+=", "*=", "..=" etc. first load the current value
3333 if (*cac->cac_op != '='
3334 && compile_load_lhs_with_index(&cac->cac_lhs, cac->cac_var_start,
3335 cctx) == FAIL)
3336 return FAIL;
3337
3338 // For "var = expr" evaluate the expression.
3339 if (cac->cac_var_count == 0)
3340 {
3341 int ret;
3342
3343 // Compile the expression.
3344 cac->cac_instr_count = cac->cac_instr->ga_len;
3345 ret = compile_assign_single_eval_expr(cctx, cac);
3346 if (ret == FAIL)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003347 return FAIL;
3348 }
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003349 else if (cac->cac_semicolon && cac->cac_var_idx == cac->cac_var_count - 1)
3350 {
3351 // For "[var; var] = expr" get the rest of the list
3352 cac->cac_did_generate_slice = TRUE;
3353 if (generate_SLICE(cctx, cac->cac_var_count - 1) == FAIL)
3354 return FAIL;
3355 }
3356 else
3357 {
3358 // For "[var, var] = expr" get the "var_idx" item from the
3359 // list.
3360 int with_op = *cac->cac_op != '=';
3361 if (generate_GETITEM(cctx, cac->cac_var_idx, with_op) == FAIL)
3362 return FAIL;
3363 }
3364
3365 if (compile_assign_check_type(cctx, cac) == FAIL)
3366 return FAIL;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003367
3368 return OK;
3369}
3370
3371/*
3372 * Compile the RHS expression in an assignment
3373 */
3374 static int
3375compile_assign_rhs(cctx_T *cctx, cac_T *cac)
3376{
3377 lhs_T *lhs = &cac->cac_lhs;
3378
3379 if (cctx->ctx_skip == SKIP_YES)
3380 {
3381 if (cac->cac_oplen > 0 && cac->cac_var_count == 0)
3382 {
3383 // skip over the "=" and the expression
3384 cac->cac_nextc = skipwhite(cac->cac_op + cac->cac_oplen);
3385 (void)compile_expr0(&cac->cac_nextc, cctx);
3386 }
3387 return OK;
3388 }
3389
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003390 // If RHS is specified, then generate instructions for RHS expression
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003391 if (cac->cac_oplen > 0)
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003392 return compile_assign_rhs_expr(cctx, cac);
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003393
3394 if (cac->cac_cmdidx == CMD_final)
3395 {
3396 emsg(_(e_final_requires_a_value));
3397 return FAIL;
3398 }
3399
3400 if (cac->cac_cmdidx == CMD_const)
3401 {
3402 emsg(_(e_const_requires_a_value));
3403 return FAIL;
3404 }
3405
3406 if (!lhs->lhs_has_type || lhs->lhs_dest == dest_option
3407 || lhs->lhs_dest == dest_func_option)
3408 {
3409 emsg(_(e_type_or_initialization_required));
3410 return FAIL;
3411 }
3412
3413 // variables are always initialized
3414 if (GA_GROW_FAILS(cac->cac_instr, 1))
3415 return FAIL;
3416
3417 cac->cac_instr_count = cac->cac_instr->ga_len;
3418
3419 return push_default_value(cctx, lhs->lhs_member_type->tt_type,
3420 lhs->lhs_dest == dest_local,
3421 &cac->cac_skip_store);
3422}
3423
3424/*
3425 * Compile a compound op assignment statement (+=, -=, *=, %=, etc.)
3426 */
3427 static int
3428compile_assign_compound_op(cctx_T *cctx, cac_T *cac)
3429{
3430 lhs_T *lhs = &cac->cac_lhs;
3431 type_T *expected;
3432 type_T *stacktype = NULL;
3433
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01003434 if (cac->cac_lhs.lhs_type->tt_type == VAR_TUPLE)
3435 {
3436 // compound operators are not supported with a tuple
3437 char_u op[2];
3438
3439 op[0] = *cac->cac_op;
3440 op[1] = NUL;
3441 semsg(_(e_wrong_variable_type_for_str_equal), op);
3442 return FAIL;
3443 }
3444
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003445 if (*cac->cac_op == '.')
3446 {
Hirohito Higashi2ffd35f2025-07-08 21:47:01 +02003447 expected = lhs->lhs_member_type;
3448 stacktype = get_type_on_stack(cctx, 0);
3449 if (expected != &t_string
3450 && need_type(stacktype, expected, FALSE, -1, 0, cctx,
3451 FALSE, FALSE) == FAIL)
3452 return FAIL;
3453 else if (may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003454 return FAIL;
3455 }
3456 else
3457 {
3458 expected = lhs->lhs_member_type;
3459 stacktype = get_type_on_stack(cctx, 0);
3460 if (
3461 // If variable is float operation with number is OK.
3462 !(expected == &t_float && (stacktype == &t_number
3463 || stacktype == &t_number_bool))
3464 && need_type(stacktype, expected, TRUE, -1, 0, cctx,
3465 FALSE, FALSE) == FAIL)
3466 return FAIL;
3467 }
3468
3469 if (*cac->cac_op == '.')
3470 {
3471 if (generate_CONCAT(cctx, 2) == FAIL)
3472 return FAIL;
3473 }
3474 else if (*cac->cac_op == '+')
3475 {
3476 if (generate_add_instr(cctx,
3477 operator_type(lhs->lhs_member_type, stacktype),
3478 lhs->lhs_member_type, stacktype,
3479 EXPR_APPEND) == FAIL)
3480 return FAIL;
3481 }
3482 else if (generate_two_op(cctx, cac->cac_op) == FAIL)
3483 return FAIL;
3484
3485 return OK;
3486}
3487
3488/*
3489 * Generate the STORE and SETTYPE instructions for an assignment statement.
3490 */
3491 static int
3492compile_assign_generate_store(cctx_T *cctx, cac_T *cac)
3493{
3494 lhs_T *lhs = &cac->cac_lhs;
3495 int save_lnum;
3496
3497 // Use the line number of the assignment for store instruction.
3498 save_lnum = cctx->ctx_lnum;
3499 cctx->ctx_lnum = cac->cac_start_lnum - 1;
3500
3501 if (lhs->lhs_has_index)
3502 {
3503 // Use the info in "lhs" to store the value at the index in the
3504 // list, dict or object.
3505 if (compile_assign_unlet(cac->cac_var_start, &cac->cac_lhs,
3506 TRUE, cac->cac_rhs_type, cctx) == FAIL)
3507 {
3508 cctx->ctx_lnum = save_lnum;
3509 return FAIL;
3510 }
3511 }
3512 else
3513 {
3514 if (cac->cac_is_decl && cac->cac_cmdidx == CMD_const &&
3515 (lhs->lhs_dest == dest_script
3516 || lhs->lhs_dest == dest_script_v9
3517 || lhs->lhs_dest == dest_global
3518 || lhs->lhs_dest == dest_local))
3519 // ":const var": lock the value, but not referenced variables
3520 generate_LOCKCONST(cctx);
3521
3522 type_T *inferred_type = cac->cac_inferred_type;
3523
3524 if ((lhs->lhs_type->tt_type == VAR_DICT
3525 || lhs->lhs_type->tt_type == VAR_LIST)
3526 && lhs->lhs_type->tt_member != NULL
3527 && lhs->lhs_type->tt_member != &t_any
3528 && lhs->lhs_type->tt_member != &t_unknown)
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01003529 // Set the type in the list or dict, so that it can be
3530 // checked, also in legacy script.
3531 generate_SETTYPE(cctx, lhs->lhs_type);
3532 else if (lhs->lhs_type->tt_type == VAR_TUPLE
3533 && lhs->lhs_type->tt_argcount != 0)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003534 generate_SETTYPE(cctx, lhs->lhs_type);
3535 else if (inferred_type != NULL
3536 && (inferred_type->tt_type == VAR_DICT
3537 || inferred_type->tt_type == VAR_LIST)
3538 && inferred_type->tt_member != NULL
3539 && inferred_type->tt_member != &t_unknown
3540 && inferred_type->tt_member != &t_any)
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01003541 // Set the type in the list or dict, so that it can be
3542 // checked, also in legacy script.
3543 generate_SETTYPE(cctx, inferred_type);
3544 else if (inferred_type != NULL
3545 && inferred_type->tt_type == VAR_TUPLE
3546 && inferred_type->tt_argcount > 0)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003547 generate_SETTYPE(cctx, inferred_type);
3548
3549 if (!cac->cac_skip_store &&
3550 generate_store_lhs(cctx, &cac->cac_lhs,
3551 cac->cac_instr_count,
3552 cac->cac_is_decl) == FAIL)
3553 {
3554 cctx->ctx_lnum = save_lnum;
3555 return FAIL;
3556 }
3557 }
3558
3559 cctx->ctx_lnum = save_lnum;
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003560
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003561 return OK;
3562}
3563
3564/*
3565 * Process the variable(s) in an assignment statement
3566 */
3567 static int
3568compile_assign_process_variables(
3569 cctx_T *cctx,
3570 cac_T *cac,
3571 int cmdidx,
3572 int heredoc,
3573 int has_cmd,
3574 int has_argisset_prefix,
3575 int jump_instr_idx)
3576{
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003577 /*
3578 * Loop over variables in "[var, var] = expr".
3579 * For "name = expr" and "var name: type" this is done only once.
3580 */
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003581 for (cac->cac_var_idx = 0; cac->cac_var_idx == 0 ||
3582 cac->cac_var_idx < cac->cac_var_count; cac->cac_var_idx++)
3583 {
3584 if (cac->cac_var_start[0] == '_'
3585 && !eval_isnamec(cac->cac_var_start[1]))
3586 {
3587 // Ignore underscore in "[a, _, b] = list".
3588 if (cac->cac_var_count > 0)
3589 {
3590 cac->cac_var_start = skipwhite(cac->cac_var_start + 2);
3591 continue;
3592 }
3593 emsg(_(e_cannot_use_underscore_here));
3594 return FAIL;
3595 }
3596 vim_free(cac->cac_lhs.lhs_name);
3597
3598 /*
3599 * Figure out the LHS type and other properties.
3600 */
3601 if (compile_assign_lhs(cac->cac_var_start, &cac->cac_lhs, cmdidx,
3602 cac->cac_is_decl, heredoc, has_cmd,
3603 cac->cac_oplen, cctx) == FAIL)
3604 return FAIL;
3605
3606 // Compile the RHS expression
3607 if (heredoc)
3608 {
3609 SOURCING_LNUM = cac->cac_start_lnum;
3610 if (cac->cac_lhs.lhs_has_type
3611 && need_type(&t_list_string, cac->cac_lhs.lhs_type,
3612 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
3613 return FAIL;
3614 }
3615 else
3616 {
3617 if (compile_assign_rhs(cctx, cac) == FAIL)
3618 return FAIL;
3619 if (cac->cac_var_count == 0)
3620 cac->cac_var_end = cac->cac_nextc;
3621 }
3622
3623 // no need to parse more when skipping
3624 if (cctx->ctx_skip == SKIP_YES)
3625 break;
3626
3627 if (cac->cac_oplen > 0 && *cac->cac_op != '=')
3628 {
3629 if (compile_assign_compound_op(cctx, cac) == FAIL)
3630 return FAIL;
3631 }
3632
3633 // generate the store instructions
3634 if (compile_assign_generate_store(cctx, cac) == FAIL)
3635 return FAIL;
3636
3637 if (cac->cac_var_idx + 1 < cac->cac_var_count)
3638 cac->cac_var_start = skipwhite(cac->cac_lhs.lhs_end + 1);
3639
3640 if (has_argisset_prefix)
3641 {
3642 // set instruction index in JUMP_IF_ARG_SET to here
3643 isn_T *isn = ((isn_T *)cac->cac_instr->ga_data) + jump_instr_idx;
3644 isn->isn_arg.jumparg.jump_where = cac->cac_instr->ga_len;
3645 }
3646 }
3647
3648 return OK;
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003649}
3650
3651/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003652 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003653 * "let name"
3654 * "var name = expr"
3655 * "final name = expr"
3656 * "const name = expr"
3657 * "name = expr"
3658 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003659 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003660 * Return NULL for an error.
3661 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003662 */
3663 static char_u *
Bram Moolenaar65b0d162022-12-13 18:43:22 +00003664compile_assignment(
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003665 char_u *arg_start,
3666 exarg_T *eap,
3667 cmdidx_T cmdidx,
3668 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669{
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003670 cac_T cac;
Bram Moolenaar65b0d162022-12-13 18:43:22 +00003671 char_u *arg = arg_start;
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003672 char_u *retstr = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673 int heredoc = FALSE;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003674 int jump_instr_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003676 compile_assign_context_init(&cac, cctx, cmdidx, arg);
3677
3678 jump_instr_idx = cac.cac_instr->ga_len;
3679
3680 // process object variable initialization in a new() constructor method
3681 int has_argisset_prefix = STRNCMP(arg, "ifargisset ", 11) == 0;
3682 if (has_argisset_prefix &&
3683 compile_assign_obj_new_arg(&arg, cctx) == FAIL)
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003684 goto theend;
Bram Moolenaar65b0d162022-12-13 18:43:22 +00003685
Bram Moolenaare1d12112022-03-05 11:37:48 +00003686 // Skip over the "varname" or "[varname, varname]" to get to any "=".
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003687 cac.cac_nextc = skip_var_list(arg, TRUE, &cac.cac_var_count,
3688 &cac.cac_semicolon, TRUE);
3689 if (cac.cac_nextc == NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003690 return *arg == '[' ? arg : NULL;
3691
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003692 if (compile_assign_process_operator(eap, arg, &cac, &heredoc,
3693 &retstr) == FAIL)
3694 return retstr;
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003695
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003696 // Compute the start of the assignment
3697 cac.cac_var_start = compile_assign_compute_start(arg, cac.cac_var_count);
3698
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003699 // Compute the end of the assignment
3700 cac.cac_var_end = compile_assign_compute_end(eap, cctx, &cac, heredoc);
3701 if (cac.cac_var_end == NULL)
3702 return NULL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003703
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003704 int has_cmd = cac.cac_var_start > eap->cmd;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003705
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003706 /* process the variable(s) */
3707 if (compile_assign_process_variables(cctx, &cac, cmdidx, heredoc,
3708 has_cmd, has_argisset_prefix,
3709 jump_instr_idx) == FAIL)
3710 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003711
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02003712 // For "[var, var] = expr" drop the "expr" value.
3713 // Also for "[var, var; _] = expr".
Yegappan Lakshmanane2038412024-12-14 19:59:24 +01003714 if (cctx->ctx_skip != SKIP_YES && cac.cac_var_count > 0 &&
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003715 (!cac.cac_semicolon || !cac.cac_did_generate_slice))
Bram Moolenaar9af78762020-06-16 11:34:42 +02003716 {
Bram Moolenaarec792292020-12-13 21:26:56 +01003717 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02003718 goto theend;
3719 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003720
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003721 retstr = skipwhite(cac.cac_var_end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003722
3723theend:
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003724 vim_free(cac.cac_lhs.lhs_name);
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003725 return retstr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003726}
3727
3728/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01003729 * Check for an assignment at "eap->cmd", compile it if found.
3730 * Return NOTDONE if there is none, FAIL for failure, OK if done.
3731 */
3732 static int
3733may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
3734{
3735 char_u *pskip;
3736 char_u *p;
3737
3738 // Assuming the command starts with a variable or function name,
3739 // find what follows.
3740 // Skip over "var.member", "var[idx]" and the like.
3741 // Also "&opt = val", "$ENV = val" and "@r = val".
3742 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
3743 ? eap->cmd + 1 : eap->cmd;
3744 p = to_name_end(pskip, TRUE);
3745 if (p > eap->cmd && *p != NUL)
3746 {
3747 char_u *var_end;
3748 int oplen;
3749 int heredoc;
3750
3751 if (eap->cmd[0] == '@')
3752 var_end = eap->cmd + 2;
3753 else
3754 var_end = find_name_end(pskip, NULL, NULL,
3755 FNE_CHECK_START | FNE_INCL_BR);
3756 oplen = assignment_len(skipwhite(var_end), &heredoc);
3757 if (oplen > 0)
3758 {
3759 size_t len = p - eap->cmd;
3760
3761 // Recognize an assignment if we recognize the variable
3762 // name:
Bram Moolenaar17126b12021-01-07 22:03:02 +01003763 // "&opt = expr"
3764 // "$ENV = expr"
3765 // "@r = expr"
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003766 // "g:var = expr"
3767 // "g:[key] = expr"
3768 // "local = expr" where "local" is a local var.
3769 // "script = expr" where "script" is a script-local var.
3770 // "import = expr" where "import" is an imported var
Bram Moolenaar17126b12021-01-07 22:03:02 +01003771 if (*eap->cmd == '&'
3772 || *eap->cmd == '$'
3773 || *eap->cmd == '@'
3774 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003775 || STRNCMP(eap->cmd, "g:[", 3) == 0
Bram Moolenaare0890d62021-02-17 14:52:14 +01003776 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01003777 {
3778 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
3779 if (*line == NULL || *line == eap->cmd)
3780 return FAIL;
3781 return OK;
3782 }
3783 }
3784 }
3785
Bram Moolenaar65b0d162022-12-13 18:43:22 +00003786 // might be "[var, var] = expr" or "ifargisset this.member = expr"
3787 if (*eap->cmd == '[' || STRNCMP(eap->cmd, "ifargisset ", 11) == 0)
Bram Moolenaar17126b12021-01-07 22:03:02 +01003788 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01003789 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
3790 if (*line == NULL)
3791 return FAIL;
3792 if (*line != eap->cmd)
3793 return OK;
3794 }
3795 return NOTDONE;
3796}
3797
Bram Moolenaar9a015112021-12-31 14:06:45 +00003798/*
3799 * Check if arguments of "ufunc" shadow variables in "cctx".
3800 * Return OK or FAIL.
3801 */
3802 static int
3803check_args_shadowing(ufunc_T *ufunc, cctx_T *cctx)
3804{
3805 int i;
3806 char_u *arg;
3807 int r = OK;
3808
3809 // Make sure arguments are not found when compiling a second time.
3810 ufunc->uf_args_visible = 0;
3811
3812 // Check for arguments shadowing variables from the context.
3813 for (i = 0; i < ufunc->uf_args.ga_len; ++i)
3814 {
3815 arg = ((char_u **)(ufunc->uf_args.ga_data))[i];
Bram Moolenaardce24412022-02-08 20:35:30 +00003816 if (check_defined(arg, STRLEN(arg), cctx, NULL, TRUE) == FAIL)
Bram Moolenaar9a015112021-12-31 14:06:45 +00003817 {
3818 r = FAIL;
3819 break;
3820 }
3821 }
3822 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
3823 return r;
3824}
3825
Bram Moolenaarfcb86b02022-10-07 22:46:24 +01003826#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar139575d2022-03-15 19:29:30 +00003827/*
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003828 * Get a count before a command. Can only be a number.
3829 * Returns zero if there is no count.
3830 * Returns -1 if there is something wrong.
3831 */
3832 static long
3833get_cmd_count(char_u *line, exarg_T *eap)
3834{
3835 char_u *p;
3836
3837 // skip over colons and white space
3838 for (p = line; *p == ':' || VIM_ISWHITE(*p); ++p)
3839 ;
Keith Thompson184f71c2024-01-04 21:19:04 +01003840 if (!SAFE_isdigit(*p))
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003841 {
Bram Moolenaar2435adf2022-10-21 12:05:46 +01003842 // The command or modifiers must be following. Assume a lower case
3843 // character means there is a modifier.
3844 if (p < eap->cmd && !vim_islower(*p))
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003845 {
3846 emsg(_(e_invalid_range));
3847 return -1;
3848 }
3849 return 0;
3850 }
3851 return atol((char *)p);
3852}
Bram Moolenaarfcb86b02022-10-07 22:46:24 +01003853#endif
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003854
3855/*
Bram Moolenaar139575d2022-03-15 19:29:30 +00003856 * Get the compilation type that should be used for "ufunc".
3857 * Keep in sync with INSTRUCTIONS().
3858 */
3859 compiletype_T
3860get_compile_type(ufunc_T *ufunc)
3861{
3862 // Update uf_has_breakpoint if needed.
3863 update_has_breakpoint(ufunc);
3864
3865 if (debug_break_level > 0 || may_break_in_function(ufunc))
3866 return CT_DEBUG;
3867#ifdef FEAT_PROFILE
3868 if (do_profiling == PROF_YES)
3869 {
Ernie Rael21d32122023-09-02 15:09:18 +02003870 if (!ufunc->uf_profiling && has_profiling(FALSE, ufunc->uf_name, NULL,
3871 &ufunc->uf_hash))
Bram Moolenaar139575d2022-03-15 19:29:30 +00003872 func_do_profile(ufunc);
3873 if (ufunc->uf_profiling)
3874 return CT_PROFILE;
3875 }
3876#endif
3877 return CT_NONE;
3878}
3879
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01003880/*
3881 * Free the compiled instructions saved for a def function. This is used when
3882 * compiling a def function and the function was compiled before.
3883 * The index is reused.
3884 */
3885 static void
3886clear_def_function(ufunc_T *ufunc, compiletype_T compile_type)
3887{
3888 isn_T *instr_dest = NULL;
3889 dfunc_T *dfunc;
3890
3891 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
3892
3893 switch (compile_type)
3894 {
3895 case CT_PROFILE:
3896#ifdef FEAT_PROFILE
3897 instr_dest = dfunc->df_instr_prof; break;
3898#endif
3899 case CT_NONE: instr_dest = dfunc->df_instr; break;
3900 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
3901 }
3902
3903 if (instr_dest != NULL)
3904 // Was compiled in this mode before: Free old instructions.
3905 delete_def_function_contents(dfunc, FALSE);
3906
3907 ga_clear_strings(&dfunc->df_var_names);
3908 dfunc->df_defer_var_idx = 0;
3909}
Bram Moolenaar7b829262021-10-13 15:04:34 +01003910
3911/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02003912 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003913 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02003914 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02003915 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02003916add_def_function(ufunc_T *ufunc)
3917{
3918 dfunc_T *dfunc;
3919
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003920 if (def_functions.ga_len == 0)
3921 {
3922 // The first position is not used, so that a zero uf_dfunc_idx means it
3923 // wasn't set.
Bram Moolenaar35578162021-08-02 19:10:38 +02003924 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003925 return FAIL;
3926 ++def_functions.ga_len;
3927 }
3928
Bram Moolenaar09689a02020-05-09 22:50:08 +02003929 // Add the function to "def_functions".
Bram Moolenaar35578162021-08-02 19:10:38 +02003930 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar09689a02020-05-09 22:50:08 +02003931 return FAIL;
3932 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
3933 CLEAR_POINTER(dfunc);
3934 dfunc->df_idx = def_functions.ga_len;
3935 ufunc->uf_dfunc_idx = dfunc->df_idx;
3936 dfunc->df_ufunc = ufunc;
John Marriottb32800f2025-02-01 15:25:34 +01003937 dfunc->df_name = vim_strnsave(ufunc->uf_name, ufunc->uf_namelen);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02003938 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003939 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02003940 ++def_functions.ga_len;
3941 return OK;
3942}
3943
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01003944 static int
3945compile_dfunc_ufunc_init(
3946 ufunc_T *ufunc,
3947 cctx_T *outer_cctx,
3948 compiletype_T compile_type,
3949 int *new_def_function)
3950{
3951 // When using a function that was compiled before: Free old instructions.
3952 // The index is reused. Otherwise add a new entry in "def_functions".
3953 if (ufunc->uf_dfunc_idx > 0)
3954 clear_def_function(ufunc, compile_type);
3955 else
3956 {
3957 if (add_def_function(ufunc) == FAIL)
3958 return FAIL;
3959
3960 *new_def_function = TRUE;
3961 }
3962
3963 if ((ufunc->uf_flags & FC_CLOSURE) && outer_cctx == NULL)
3964 {
3965 semsg(_(e_compiling_closure_without_context_str),
3966 printable_func_name(ufunc));
3967 return FAIL;
3968 }
3969
3970 ufunc->uf_def_status = UF_COMPILING;
3971
3972 return OK;
3973}
3974
3975/*
3976 * Initialize the compilation context for compiling a def function.
3977 */
3978 static void
3979compile_dfunc_cctx_init(
3980 cctx_T *cctx,
3981 cctx_T *outer_cctx,
3982 ufunc_T *ufunc,
3983 compiletype_T compile_type)
3984{
3985 CLEAR_FIELD(*cctx);
3986
3987 cctx->ctx_compile_type = compile_type;
3988 cctx->ctx_ufunc = ufunc;
3989 cctx->ctx_lnum = -1;
3990 cctx->ctx_outer = outer_cctx;
3991 ga_init2(&cctx->ctx_locals, sizeof(lvar_T), 10);
3992 // Each entry on the type stack consists of two type pointers.
3993 ga_init2(&cctx->ctx_type_stack, sizeof(type2_T), 50);
3994 cctx->ctx_type_list = &ufunc->uf_type_list;
3995 ga_init2(&cctx->ctx_instr, sizeof(isn_T), 50);
3996}
3997
Bram Moolenaar09689a02020-05-09 22:50:08 +02003998/*
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02003999 * For an object constructor, generate instruction to setup "this" (the first
4000 * local variable) and to initialize the object variables.
4001 */
4002 static int
4003obj_constructor_prologue(ufunc_T *ufunc, cctx_T *cctx)
4004{
4005 generate_CONSTRUCT(cctx, ufunc->uf_class);
4006
4007 for (int i = 0; i < ufunc->uf_class->class_obj_member_count; ++i)
4008 {
4009 ocmember_T *m = &ufunc->uf_class->class_obj_members[i];
4010
4011 if (i < 2 && IS_ENUM(ufunc->uf_class))
4012 // The first two object variables in an enum are the name
4013 // and the ordinal. These are set by the ISN_CONSTRUCT
4014 // instruction. So don't generate instructions to set
4015 // these variables.
4016 continue;
4017
4018 if (m->ocm_init != NULL)
4019 {
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +01004020 char_u *expr = m->ocm_init;
4021 sctx_T save_current_sctx;
4022 int change_sctx = FALSE;
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004023
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +01004024 // If the member variable initialization script context is
4025 // different from the current script context, then change it.
4026 if (current_sctx.sc_sid != m->ocm_init_sctx.sc_sid)
4027 change_sctx = TRUE;
4028
4029 if (change_sctx)
4030 {
4031 // generate an instruction to change the script context to the
4032 // member variable initialization script context.
4033 save_current_sctx = current_sctx;
4034 current_sctx = m->ocm_init_sctx;
4035 generate_SCRIPTCTX_SET(cctx, current_sctx);
4036 }
4037
4038 int r = compile_expr0(&expr, cctx);
4039
4040 if (change_sctx)
4041 {
4042 // restore the previous script context
4043 current_sctx = save_current_sctx;
4044 generate_SCRIPTCTX_SET(cctx, current_sctx);
4045 }
4046
4047 if (r == FAIL)
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004048 return FAIL;
4049
4050 if (!ends_excmd2(m->ocm_init, expr))
4051 {
4052 semsg(_(e_trailing_characters_str), expr);
4053 return FAIL;
4054 }
4055
4056 type_T *type = get_type_on_stack(cctx, 0);
4057 if (m->ocm_type->tt_type == VAR_ANY
4058 && !(m->ocm_flags & OCMFLAG_HAS_TYPE)
4059 && type->tt_type != VAR_SPECIAL)
4060 {
4061 // If the member variable type is not yet set, then use
4062 // the initialization expression type.
4063 m->ocm_type = type;
4064 }
LemonBoyf4af3312024-07-04 13:43:12 +02004065 else
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004066 {
4067 // The type of the member initialization expression is
4068 // determined at run time. Add a runtime type check.
4069 where_T where = WHERE_INIT;
4070 where.wt_kind = WT_MEMBER;
4071 where.wt_func_name = (char *)m->ocm_name;
4072 if (need_type_where(type, m->ocm_type, FALSE, -1,
4073 where, cctx, FALSE, FALSE) == FAIL)
4074 return FAIL;
4075 }
4076 }
4077 else
4078 push_default_value(cctx, m->ocm_type->tt_type, FALSE, NULL);
4079
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01004080 if (((m->ocm_type->tt_type == VAR_DICT
4081 || m->ocm_type->tt_type == VAR_LIST)
4082 && m->ocm_type->tt_member != NULL
4083 && m->ocm_type->tt_member != &t_any
4084 && m->ocm_type->tt_member != &t_unknown)
4085 || (m->ocm_type->tt_type == VAR_TUPLE
4086 && m->ocm_type->tt_argcount > 0))
4087 // Set the type in the list, tuple or dict, so that it can be
4088 // checked, also in legacy script.
LemonBoyf4af3312024-07-04 13:43:12 +02004089 generate_SETTYPE(cctx, m->ocm_type);
4090
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004091 generate_STORE_THIS(cctx, i);
4092 }
4093
4094 return OK;
4095}
4096
4097/*
4098 * For an object method and an constructor, generate instruction to setup
4099 * "this" (the first local variable). For a constructor, generate instructions
4100 * to initialize the object variables.
4101 */
4102 static int
4103obj_method_prologue(ufunc_T *ufunc, cctx_T *cctx)
4104{
4105 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
4106
4107 if (GA_GROW_FAILS(&dfunc->df_var_names, 1))
4108 return FAIL;
4109
4110 ((char_u **)dfunc->df_var_names.ga_data)[0] =
4111 vim_strsave((char_u *)"this");
4112 ++dfunc->df_var_names.ga_len;
4113
4114 // In the constructor allocate memory for the object and initialize the
4115 // object members.
4116 if (IS_CONSTRUCTOR_METHOD(ufunc))
4117 return obj_constructor_prologue(ufunc, cctx);
4118
4119 return OK;
4120}
4121
4122/*
4123 * Produce instructions for the default values of optional arguments.
4124 */
4125 static int
4126compile_def_function_default_args(
4127 ufunc_T *ufunc,
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004128 garray_T *instr,
4129 cctx_T *cctx)
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004130{
4131 int count = ufunc->uf_def_args.ga_len;
4132 int first_def_arg = ufunc->uf_args.ga_len - count;
4133 int i;
4134 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
4135 int did_set_arg_type = FALSE;
4136
4137 // Produce instructions for the default values of optional arguments.
4138 SOURCING_LNUM = 0; // line number unknown
4139 for (i = 0; i < count; ++i)
4140 {
4141 char_u *arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
4142 if (STRCMP(arg, "v:none") == 0)
4143 // "arg = v:none" means the argument is optional without
4144 // setting a value when the argument is missing.
4145 continue;
4146
4147 type_T *val_type;
4148 int arg_idx = first_def_arg + i;
4149 where_T where = WHERE_INIT;
4150 int jump_instr_idx = instr->ga_len;
4151 isn_T *isn;
4152
4153 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
4154 if (generate_JUMP_IF_ARG(cctx, ISN_JUMP_IF_ARG_SET,
4155 i - count - off) == FAIL)
4156 return FAIL;
4157
4158 // Make sure later arguments are not found.
4159 ufunc->uf_args_visible = arg_idx;
4160
4161 int r = compile_expr0(&arg, cctx);
4162 if (r == FAIL)
4163 return FAIL;
4164
4165 // If no type specified use the type of the default value.
4166 // Otherwise check that the default value type matches the
4167 // specified type.
4168 val_type = get_type_on_stack(cctx, 0);
4169 where.wt_index = arg_idx + 1;
4170 where.wt_kind = WT_ARGUMENT;
4171 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
4172 {
4173 did_set_arg_type = TRUE;
4174 ufunc->uf_arg_types[arg_idx] = val_type;
4175 }
4176 else if (need_type_where(val_type, ufunc->uf_arg_types[arg_idx],
4177 FALSE, -1, where, cctx, FALSE, FALSE) == FAIL)
4178 return FAIL;
4179
4180 if (generate_STORE(cctx, ISN_STORE, i - count - off, NULL) == FAIL)
4181 return FAIL;
4182
4183 // set instruction index in JUMP_IF_ARG_SET to here
4184 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
4185 isn->isn_arg.jumparg.jump_where = instr->ga_len;
4186 }
4187
4188 if (did_set_arg_type)
4189 set_function_type(ufunc);
4190
4191 return OK;
4192}
4193
4194/*
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004195 * Compile def function body. Loop over all the lines in the function and
4196 * generate instructions.
4197 */
4198 static int
4199compile_def_function_body(
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004200 int last_func_lnum,
4201 int check_return_type,
4202 garray_T *lines_to_free,
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004203 char **errormsg,
4204 cctx_T *cctx)
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004205{
4206 char_u *line = NULL;
4207 char_u *p;
4208 int did_emsg_before = did_emsg;
4209#ifdef FEAT_PROFILE
4210 int prof_lnum = -1;
4211#endif
4212 int debug_lnum = -1;
4213
4214 for (;;)
4215 {
4216 exarg_T ea;
4217 int starts_with_colon = FALSE;
4218 char_u *cmd;
4219 cmdmod_T local_cmdmod;
4220
4221 // Bail out on the first error to avoid a flood of errors and report
4222 // the right line number when inside try/catch.
4223 if (did_emsg_before != did_emsg)
4224 return FAIL;
4225
4226 if (line != NULL && *line == '|')
4227 // the line continues after a '|'
4228 ++line;
4229 else if (line != NULL && *skipwhite(line) != NUL
4230 && !(*line == '#' && (line == cctx->ctx_line_start
4231 || VIM_ISWHITE(line[-1]))))
4232 {
4233 semsg(_(e_trailing_characters_str), line);
4234 return FAIL;
4235 }
4236 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
4237 return FAIL;
4238 else
4239 {
4240 line = next_line_from_context(cctx, FALSE);
4241 if (cctx->ctx_lnum >= last_func_lnum)
4242 {
4243 // beyond the last line
4244#ifdef FEAT_PROFILE
4245 if (cctx->ctx_skip != SKIP_YES)
4246 may_generate_prof_end(cctx, prof_lnum);
4247#endif
4248 break;
4249 }
4250 // Make a copy, splitting off nextcmd and removing trailing spaces
4251 // may change it.
4252 if (line != NULL)
4253 {
4254 line = vim_strsave(line);
4255 if (ga_add_string(lines_to_free, line) == FAIL)
4256 return FAIL;
4257 }
4258 }
4259
4260 CLEAR_FIELD(ea);
4261 ea.cmdlinep = &line;
4262 ea.cmd = skipwhite(line);
4263 ea.skip = cctx->ctx_skip == SKIP_YES;
4264
4265 if (*ea.cmd == '#')
4266 {
4267 // "#" starts a comment, but "#{" is an error
4268 if (vim9_bad_comment(ea.cmd))
4269 return FAIL;
4270 line = (char_u *)"";
4271 continue;
4272 }
4273
4274#ifdef FEAT_PROFILE
4275 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_lnum != prof_lnum
4276 && cctx->ctx_skip != SKIP_YES)
4277 {
4278 may_generate_prof_end(cctx, prof_lnum);
4279
4280 prof_lnum = cctx->ctx_lnum;
4281 generate_instr(cctx, ISN_PROF_START);
4282 }
4283#endif
4284 if (cctx->ctx_compile_type == CT_DEBUG && cctx->ctx_lnum != debug_lnum
4285 && cctx->ctx_skip != SKIP_YES)
4286 {
4287 debug_lnum = cctx->ctx_lnum;
4288 generate_instr_debug(cctx);
4289 }
4290 cctx->ctx_prev_lnum = cctx->ctx_lnum + 1;
4291
4292 // Some things can be recognized by the first character.
4293 switch (*ea.cmd)
4294 {
4295 case '}':
4296 {
4297 // "}" ends a block scope
4298 scopetype_T stype = cctx->ctx_scope == NULL
4299 ? NO_SCOPE : cctx->ctx_scope->se_type;
4300
4301 if (stype == BLOCK_SCOPE)
4302 {
4303 compile_endblock(cctx);
4304 line = ea.cmd;
4305 }
4306 else
4307 {
4308 emsg(_(e_using_rcurly_outside_if_block_scope));
4309 return FAIL;
4310 }
4311 if (line != NULL)
4312 line = skipwhite(ea.cmd + 1);
4313 continue;
4314 }
4315
4316 case '{':
4317 // "{" starts a block scope
4318 // "{'a': 1}->func() is something else
4319 if (ends_excmd(*skipwhite(ea.cmd + 1)))
4320 {
4321 line = compile_block(ea.cmd, cctx);
4322 continue;
4323 }
4324 break;
4325 }
4326
4327 /*
4328 * COMMAND MODIFIERS
4329 */
4330 cctx->ctx_has_cmdmod = FALSE;
4331 if (parse_command_modifiers(&ea, errormsg, &local_cmdmod, FALSE)
4332 == FAIL)
4333 return FAIL;
4334 generate_cmdmods(cctx, &local_cmdmod);
4335 undo_cmdmod(&local_cmdmod);
4336
4337 // Check if there was a colon after the last command modifier or before
4338 // the current position.
4339 for (p = ea.cmd; p >= line; --p)
4340 {
4341 if (*p == ':')
4342 starts_with_colon = TRUE;
4343 if (p < ea.cmd && !VIM_ISWHITE(*p))
4344 break;
4345 }
4346
4347 // Skip ":call" to get to the function name, unless using :legacy
4348 p = ea.cmd;
4349 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
4350 {
4351 if (checkforcmd(&ea.cmd, "call", 3))
4352 {
4353 if (*ea.cmd == '(')
4354 // not for "call()"
4355 ea.cmd = p;
4356 else
4357 ea.cmd = skipwhite(ea.cmd);
4358 }
4359
4360 if (!starts_with_colon)
4361 {
4362 int assign;
4363
4364 // Check for assignment after command modifiers.
4365 assign = may_compile_assignment(&ea, &line, cctx);
4366 if (assign == OK)
4367 goto nextline;
4368 if (assign == FAIL)
4369 return FAIL;
4370 }
4371 }
4372
4373 /*
4374 * COMMAND after range
4375 * 'text'->func() should not be confused with 'a mark
4376 * 0z1234->func() should not be confused with a zero line number
4377 * "++nr" and "--nr" are eval commands
4378 * in "$ENV->func()" the "$" is not a range
4379 * "123->func()" is a method call
4380 */
4381 cmd = ea.cmd;
4382 if ((*cmd != '$' || starts_with_colon)
4383 && (starts_with_colon
4384 || !(*cmd == '\''
4385 || (cmd[0] == '0' && cmd[1] == 'z')
4386 || (cmd[0] != NUL && cmd[0] == cmd[1]
4387 && (*cmd == '+' || *cmd == '-'))
4388 || number_method(cmd))))
4389 {
4390 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
4391 if (ea.cmd > cmd)
4392 {
4393 if (!starts_with_colon
4394 && !(local_cmdmod.cmod_flags & CMOD_LEGACY))
4395 {
4396 semsg(_(e_colon_required_before_range_str), cmd);
4397 return FAIL;
4398 }
4399 ea.addr_count = 1;
4400 if (ends_excmd2(line, ea.cmd))
4401 {
4402 // A range without a command: jump to the line.
4403 generate_EXEC(cctx, ISN_EXECRANGE,
4404 vim_strnsave(cmd, ea.cmd - cmd));
4405 line = ea.cmd;
4406 goto nextline;
4407 }
4408 }
4409 }
4410 p = find_ex_command(&ea, NULL,
4411 starts_with_colon || (local_cmdmod.cmod_flags & CMOD_LEGACY)
4412 ? NULL : item_exists, cctx);
4413
4414 if (p == NULL)
4415 {
4416 if (cctx->ctx_skip != SKIP_YES)
4417 semsg(_(e_ambiguous_use_of_user_defined_command_str), ea.cmd);
4418 return FAIL;
4419 }
4420
4421 // When using ":legacy cmd" always use compile_exec().
4422 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
4423 {
4424 char_u *start = ea.cmd;
4425
4426 switch (ea.cmdidx)
4427 {
4428 case CMD_if:
4429 case CMD_elseif:
4430 case CMD_else:
4431 case CMD_endif:
4432 case CMD_for:
4433 case CMD_endfor:
4434 case CMD_continue:
4435 case CMD_break:
4436 case CMD_while:
4437 case CMD_endwhile:
4438 case CMD_try:
4439 case CMD_catch:
4440 case CMD_finally:
4441 case CMD_endtry:
4442 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
4443 return FAIL;
4444 default: break;
4445 }
4446
4447 // ":legacy return expr" needs to be handled differently.
4448 if (checkforcmd(&start, "return", 4))
4449 ea.cmdidx = CMD_return;
4450 else
4451 ea.cmdidx = CMD_legacy;
4452 }
4453
4454 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
4455 {
4456 // "eval" is used for "val->func()" and "var" for "var = val", then
4457 // "p" is equal to "ea.cmd" for a valid command.
4458 if (ea.cmdidx == CMD_eval || ea.cmdidx == CMD_var)
4459 ;
4460 else if (cctx->ctx_skip == SKIP_YES)
4461 {
4462 line += STRLEN(line);
4463 goto nextline;
4464 }
4465 else
4466 {
4467 semsg(_(e_command_not_recognized_str), ea.cmd);
4468 return FAIL;
4469 }
4470 }
4471
4472 if ((cctx->ctx_had_return || cctx->ctx_had_throw)
4473 && ea.cmdidx != CMD_elseif
4474 && ea.cmdidx != CMD_else
4475 && ea.cmdidx != CMD_endif
4476 && ea.cmdidx != CMD_endfor
4477 && ea.cmdidx != CMD_endwhile
4478 && ea.cmdidx != CMD_catch
4479 && ea.cmdidx != CMD_finally
4480 && ea.cmdidx != CMD_endtry
4481 && !ignore_unreachable_code_for_testing)
4482 {
4483 semsg(_(e_unreachable_code_after_str),
4484 cctx->ctx_had_return ? "return" : "throw");
4485 return FAIL;
4486 }
Yegappan Lakshmananab9a8942024-12-30 09:56:34 +01004487
4488 // When processing the end of an if-else block, don't clear the
4489 // "ctx_had_throw" flag. If an if-else block ends in a "throw"
4490 // statement, then it is considered to end in a "return" statement.
4491 // The "ctx_had_throw" is cleared immediately after processing the
4492 // if-else block ending statement.
4493 // Otherwise, clear the "had_throw" flag.
4494 if (ea.cmdidx != CMD_else && ea.cmdidx != CMD_elseif
4495 && ea.cmdidx != CMD_endif)
4496 cctx->ctx_had_throw = FALSE;
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004497
4498 p = skipwhite(p);
4499 if (ea.cmdidx != CMD_SIZE
4500 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
4501 {
4502 if (ea.cmdidx >= 0)
4503 ea.argt = excmd_get_argt(ea.cmdidx);
4504 if ((ea.argt & EX_BANG) && *p == '!')
4505 {
4506 ea.forceit = TRUE;
4507 p = skipwhite(p + 1);
4508 }
4509 if ((ea.argt & EX_RANGE) == 0 && ea.addr_count > 0)
4510 {
4511 emsg(_(e_no_range_allowed));
4512 return FAIL;
4513 }
4514 }
4515
4516 switch (ea.cmdidx)
4517 {
4518 case CMD_def:
4519 case CMD_function:
4520 ea.arg = p;
4521 line = compile_nested_function(&ea, cctx, lines_to_free);
4522 break;
4523
4524 case CMD_return:
4525 line = compile_return(p, check_return_type,
4526 local_cmdmod.cmod_flags & CMOD_LEGACY, cctx);
4527 cctx->ctx_had_return = TRUE;
4528 break;
4529
4530 case CMD_let:
4531 emsg(_(e_cannot_use_let_in_vim9_script));
4532 break;
4533 case CMD_var:
4534 case CMD_final:
4535 case CMD_const:
4536 case CMD_increment:
4537 case CMD_decrement:
4538 line = compile_assignment(p, &ea, ea.cmdidx, cctx);
4539 if (line == p)
4540 {
4541 emsg(_(e_invalid_assignment));
4542 line = NULL;
4543 }
4544 break;
4545
4546 case CMD_unlet:
4547 case CMD_unlockvar:
4548 case CMD_lockvar:
4549 line = compile_unletlock(p, &ea, cctx);
4550 break;
4551
4552 case CMD_import:
4553 emsg(_(e_import_can_only_be_used_in_script));
4554 line = NULL;
4555 break;
4556
4557 case CMD_if:
4558 line = compile_if(p, cctx);
4559 break;
4560 case CMD_elseif:
4561 line = compile_elseif(p, cctx);
4562 cctx->ctx_had_return = FALSE;
Yegappan Lakshmananab9a8942024-12-30 09:56:34 +01004563 cctx->ctx_had_throw = FALSE;
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004564 break;
4565 case CMD_else:
4566 line = compile_else(p, cctx);
4567 cctx->ctx_had_return = FALSE;
Yegappan Lakshmananab9a8942024-12-30 09:56:34 +01004568 cctx->ctx_had_throw = FALSE;
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004569 break;
4570 case CMD_endif:
4571 line = compile_endif(p, cctx);
Yegappan Lakshmananab9a8942024-12-30 09:56:34 +01004572 cctx->ctx_had_throw = FALSE;
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004573 break;
4574
4575 case CMD_while:
4576 line = compile_while(p, cctx);
4577 break;
4578 case CMD_endwhile:
4579 line = compile_endwhile(p, cctx);
4580 cctx->ctx_had_return = FALSE;
4581 break;
4582
4583 case CMD_for:
4584 line = compile_for(p, cctx);
4585 break;
4586 case CMD_endfor:
4587 line = compile_endfor(p, cctx);
4588 cctx->ctx_had_return = FALSE;
4589 break;
4590 case CMD_continue:
4591 line = compile_continue(p, cctx);
4592 break;
4593 case CMD_break:
4594 line = compile_break(p, cctx);
4595 break;
4596
4597 case CMD_try:
4598 line = compile_try(p, cctx);
4599 break;
4600 case CMD_catch:
4601 line = compile_catch(p, cctx);
4602 cctx->ctx_had_return = FALSE;
4603 break;
4604 case CMD_finally:
4605 line = compile_finally(p, cctx);
4606 cctx->ctx_had_return = FALSE;
4607 break;
4608 case CMD_endtry:
4609 line = compile_endtry(p, cctx);
4610 break;
4611 case CMD_throw:
4612 line = compile_throw(p, cctx);
4613 cctx->ctx_had_throw = TRUE;
4614 break;
4615
4616 case CMD_eval:
4617 line = compile_eval(p, cctx);
4618 break;
4619
4620 case CMD_defer:
4621 line = compile_defer(p, cctx);
4622 break;
4623
4624#ifdef HAS_MESSAGE_WINDOW
4625 case CMD_echowindow:
4626 {
4627 long cmd_count = get_cmd_count(line, &ea);
4628 if (cmd_count < 0)
4629 line = NULL;
4630 else
4631 line = compile_mult_expr(p, ea.cmdidx,
4632 cmd_count, cctx);
4633 }
4634 break;
4635#endif
4636 case CMD_echo:
4637 case CMD_echon:
4638 case CMD_echoconsole:
4639 case CMD_echoerr:
4640 case CMD_echomsg:
4641 case CMD_execute:
4642 line = compile_mult_expr(p, ea.cmdidx, 0, cctx);
4643 break;
4644
4645 case CMD_put:
4646 ea.cmd = cmd;
64-bitmane08f10a2025-03-18 22:14:34 +01004647 line = compile_put(p, &ea, cctx, FALSE);
4648 break;
4649
4650 case CMD_iput:
4651 ea.cmd = cmd;
4652 line = compile_put(p, &ea, cctx, TRUE);
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004653 break;
4654
4655 case CMD_substitute:
4656 if (check_global_and_subst(ea.cmd, p) == FAIL)
4657 return FAIL;
4658 if (cctx->ctx_skip == SKIP_YES)
4659 line = (char_u *)"";
4660 else
4661 {
4662 ea.arg = p;
4663 line = compile_substitute(line, &ea, cctx);
4664 }
4665 break;
4666
4667 case CMD_redir:
4668 ea.arg = p;
4669 line = compile_redir(line, &ea, cctx);
4670 break;
4671
4672 case CMD_cexpr:
4673 case CMD_lexpr:
4674 case CMD_caddexpr:
4675 case CMD_laddexpr:
4676 case CMD_cgetexpr:
4677 case CMD_lgetexpr:
4678#ifdef FEAT_QUICKFIX
4679 ea.arg = p;
4680 line = compile_cexpr(line, &ea, cctx);
4681#else
4682 ex_ni(&ea);
4683 line = NULL;
4684#endif
4685 break;
4686
4687 case CMD_append:
4688 case CMD_change:
4689 case CMD_insert:
4690 case CMD_k:
4691 case CMD_t:
4692 case CMD_xit:
4693 not_in_vim9(&ea);
4694 return FAIL;
4695
4696 case CMD_SIZE:
4697 if (cctx->ctx_skip != SKIP_YES)
4698 {
4699 semsg(_(e_invalid_command_str), ea.cmd);
4700 return FAIL;
4701 }
4702 // We don't check for a next command here.
4703 line = (char_u *)"";
4704 break;
4705
4706 case CMD_lua:
4707 case CMD_mzscheme:
4708 case CMD_perl:
4709 case CMD_py3:
4710 case CMD_python3:
4711 case CMD_python:
4712 case CMD_pythonx:
4713 case CMD_ruby:
4714 case CMD_tcl:
4715 ea.arg = p;
4716 if (vim_strchr(line, '\n') == NULL)
4717 line = compile_exec(line, &ea, cctx);
4718 else
4719 // heredoc lines have been concatenated with NL
4720 // characters in get_function_body()
4721 line = compile_script(line, cctx);
4722 break;
4723
4724 case CMD_vim9script:
4725 if (cctx->ctx_skip != SKIP_YES)
4726 {
4727 emsg(_(e_vim9script_can_only_be_used_in_script));
4728 return FAIL;
4729 }
4730 line = (char_u *)"";
4731 break;
4732
4733 case CMD_class:
4734 emsg(_(e_class_can_only_be_used_in_script));
4735 return FAIL;
4736
4737 case CMD_type:
4738 emsg(_(e_type_can_only_be_used_in_script));
4739 return FAIL;
4740
4741 case CMD_global:
4742 if (check_global_and_subst(ea.cmd, p) == FAIL)
4743 return FAIL;
4744 // FALLTHROUGH
4745 default:
4746 // Not recognized, execute with do_cmdline_cmd().
4747 ea.arg = p;
4748 line = compile_exec(line, &ea, cctx);
4749 break;
4750 }
4751nextline:
4752 if (line == NULL)
4753 return FAIL;
4754 line = skipwhite(line);
4755
4756 // Undo any command modifiers.
4757 generate_undo_cmdmods(cctx);
4758
4759 if (cctx->ctx_type_stack.ga_len < 0)
4760 {
4761 iemsg("Type stack underflow");
4762 return FAIL;
4763 }
4764 } // END of the loop over all the function body lines.
4765
4766 return OK;
4767}
4768
4769/*
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004770 * Returns TRUE if the end of a scope (if, while, for, block) is missing.
4771 * Called after compiling a def function body.
4772 */
4773 static int
4774compile_dfunc_scope_end_missing(cctx_T *cctx)
4775{
4776 if (cctx->ctx_scope == NULL)
4777 return FALSE;
4778
4779 if (cctx->ctx_scope->se_type == IF_SCOPE)
4780 emsg(_(e_missing_endif));
4781 else if (cctx->ctx_scope->se_type == WHILE_SCOPE)
4782 emsg(_(e_missing_endwhile));
4783 else if (cctx->ctx_scope->se_type == FOR_SCOPE)
4784 emsg(_(e_missing_endfor));
4785 else
4786 emsg(_(e_missing_rcurly));
4787
4788 return TRUE;
4789}
4790
4791/*
Yegappan Lakshmananab9a8942024-12-30 09:56:34 +01004792 * When compiling a def function, if it doesn't have an explicit return
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004793 * statement, then generate a default return instruction. For an object
4794 * constructor, return the object.
4795 */
4796 static int
4797compile_dfunc_generate_default_return(ufunc_T *ufunc, cctx_T *cctx)
4798{
4799 // TODO: if a function ends in "throw" but there was a return elsewhere we
4800 // should not assume the return type is "void".
4801 if (cctx->ctx_had_return || cctx->ctx_had_throw)
4802 return OK;
4803
4804 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
4805 ufunc->uf_ret_type = &t_void;
4806 else if (ufunc->uf_ret_type->tt_type != VAR_VOID
4807 && !IS_CONSTRUCTOR_METHOD(ufunc))
4808 {
4809 emsg(_(e_missing_return_statement));
4810 return FAIL;
4811 }
4812
4813 // Return void if there is no return at the end.
4814 // For a constructor return the object.
4815 if (IS_CONSTRUCTOR_METHOD(ufunc))
4816 {
4817 generate_instr(cctx, ISN_RETURN_OBJECT);
4818 ufunc->uf_ret_type = &ufunc->uf_class->class_object_type;
4819 }
4820 else
4821 generate_instr(cctx, ISN_RETURN_VOID);
4822
4823 return OK;
4824}
4825
4826/*
4827 * Perform the chores after successfully compiling a def function.
4828 */
4829 static void
4830compile_dfunc_epilogue(
4831 cctx_T *outer_cctx,
4832 ufunc_T *ufunc,
4833 garray_T *instr,
4834 cctx_T *cctx)
4835{
4836 dfunc_T *dfunc;
4837
4838 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
4839 dfunc->df_deleted = FALSE;
4840 dfunc->df_script_seq = current_sctx.sc_seq;
4841
4842#ifdef FEAT_PROFILE
4843 if (cctx->ctx_compile_type == CT_PROFILE)
4844 {
4845 dfunc->df_instr_prof = instr->ga_data;
4846 dfunc->df_instr_prof_count = instr->ga_len;
4847 }
4848 else
4849#endif
4850 if (cctx->ctx_compile_type == CT_DEBUG)
4851 {
4852 dfunc->df_instr_debug = instr->ga_data;
4853 dfunc->df_instr_debug_count = instr->ga_len;
4854 }
4855 else
4856 {
4857 dfunc->df_instr = instr->ga_data;
4858 dfunc->df_instr_count = instr->ga_len;
4859 }
4860 dfunc->df_varcount = dfunc->df_var_names.ga_len;
4861 dfunc->df_has_closure = cctx->ctx_has_closure;
4862
4863 if (cctx->ctx_outer_used)
4864 {
4865 ufunc->uf_flags |= FC_CLOSURE;
4866 if (outer_cctx != NULL)
4867 ++outer_cctx->ctx_closure_count;
4868 }
4869
4870 ufunc->uf_def_status = UF_COMPILED;
4871}
4872
4873/*
4874 * Perform the cleanup when a def function compilation fails.
4875 */
4876 static void
4877compile_dfunc_ufunc_cleanup(
4878 ufunc_T *ufunc,
4879 garray_T *instr,
4880 int new_def_function,
4881 char *errormsg,
4882 int did_emsg_before,
4883 cctx_T *cctx)
4884{
4885 dfunc_T *dfunc;
4886
4887 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
4888
4889 // Compiling aborted, free the generated instructions.
4890 clear_instr_ga(instr);
4891 VIM_CLEAR(dfunc->df_name);
4892 ga_clear_strings(&dfunc->df_var_names);
4893
4894 // If using the last entry in the table and it was added above, we
4895 // might as well remove it.
4896 if (!dfunc->df_deleted && new_def_function
4897 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
4898 {
4899 --def_functions.ga_len;
4900 ufunc->uf_dfunc_idx = 0;
4901 }
4902 ufunc->uf_def_status = UF_COMPILE_ERROR;
4903
4904 while (cctx->ctx_scope != NULL)
4905 drop_scope(cctx);
4906
4907 if (errormsg != NULL)
4908 emsg(errormsg);
4909 else if (did_emsg == did_emsg_before)
4910 emsg(_(e_compiling_def_function_failed));
4911}
4912
4913/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004914 * After ex_function() has collected all the function lines: parse and compile
4915 * the lines into instructions.
4916 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004917 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
4918 * the return statement (used for lambda). When uf_ret_type is already set
4919 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01004920 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004921 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02004922 * This can be used recursively through compile_lambda(), which may reallocate
4923 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02004924 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004925 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02004926 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01004927compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02004928 ufunc_T *ufunc,
4929 int check_return_type,
4930 compiletype_T compile_type,
4931 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004932{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004933 garray_T lines_to_free;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004934 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004935 cctx_T cctx;
4936 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01004937 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02004938 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004939 int ret = FAIL;
4940 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02004941 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02004942 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02004943 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02004944 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004945
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004946 // allocated lines are freed at the end
4947 ga_init2(&lines_to_free, sizeof(char_u *), 50);
4948
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004949 // Initialize the ufunc and the compilation context
4950 if (compile_dfunc_ufunc_init(ufunc, outer_cctx, compile_type,
4951 &new_def_function) == FAIL)
Bram Moolenaar96923b72022-03-15 15:57:04 +00004952 return FAIL;
Bram Moolenaar96923b72022-03-15 15:57:04 +00004953
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004954 compile_dfunc_cctx_init(&cctx, outer_cctx, ufunc, compile_type);
Bram Moolenaar985116a2020-07-12 17:31:09 +02004955
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004956 instr = &cctx.ctx_instr;
4957
Bram Moolenaar25e0f582020-05-25 22:36:50 +02004958 // Set the context to the function, it may be compiled when called from
4959 // another script. Set the script version to the most modern one.
4960 // The line number will be set in next_line_from_context().
4961 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004962 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4963
Bram Moolenaardc4c2302021-04-25 13:54:42 +02004964 // Don't use the flag from ":legacy" here.
4965 cmdmod.cmod_flags &= ~CMOD_LEGACY;
4966
Bram Moolenaar25e0f582020-05-25 22:36:50 +02004967 // Make sure error messages are OK.
4968 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
4969 if (do_estack_push)
4970 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02004971 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02004972
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004973 // Make sure arguments don't shadow variables in the context
Bram Moolenaar9a015112021-12-31 14:06:45 +00004974 if (check_args_shadowing(ufunc, &cctx) == FAIL)
4975 goto erret;
4976
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004977 // For an object method and a constructor generate instructions to
4978 // initialize "this" and the object variables.
Bram Moolenaar574950d2023-01-03 19:08:50 +00004979 if (ufunc->uf_flags & (FC_OBJECT|FC_NEW))
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004980 if (obj_method_prologue(ufunc, &cctx) == FAIL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004981 goto erret;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004982
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004983 if (ufunc->uf_def_args.ga_len > 0)
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004984 if (compile_def_function_default_args(ufunc, instr, &cctx) == FAIL)
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004985 goto erret;
Bram Moolenaare28d9b32021-07-03 18:56:53 +02004986 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004987
Ernie Rael7c92e882025-01-18 17:26:39 +01004988 // Compiling an abstract method or a function in an interface is done to
4989 // get the function type. No code is actually compiled.
4990 if (ufunc->uf_class != NULL && (IS_INTERFACE(ufunc->uf_class)
4991 || IS_ABSTRACT_METHOD(ufunc)))
Bram Moolenaarb8bebd02023-01-30 20:24:23 +00004992 {
4993 ufunc->uf_def_status = UF_NOT_COMPILED;
4994 ret = OK;
4995 goto erret;
4996 }
4997
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004998 // compile the function body
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004999 if (compile_def_function_body(ufunc->uf_lines.ga_len, check_return_type,
5000 &lines_to_free, &errormsg, &cctx) == FAIL)
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02005001 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005002
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01005003 if (compile_dfunc_scope_end_missing(&cctx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005004 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005005
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01005006 if (compile_dfunc_generate_default_return(ufunc, &cctx) == FAIL)
5007 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005008
Bram Moolenaar599410c2021-04-10 14:03:43 +02005009 // When compiled with ":silent!" and there was an error don't consider the
5010 // function compiled.
5011 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01005012 compile_dfunc_epilogue(outer_cctx, ufunc, instr, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005013
5014 ret = OK;
5015
5016erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02005017 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005018 {
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01005019 // compilation failed. do cleanup.
5020 compile_dfunc_ufunc_cleanup(ufunc, instr, new_def_function,
5021 errormsg, did_emsg_before, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005022 }
5023
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005024 if (cctx.ctx_redir_lhs.lhs_name != NULL)
5025 {
5026 if (ret == OK)
5027 {
5028 emsg(_(e_missing_redir_end));
5029 ret = FAIL;
5030 }
5031 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02005032 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005033 }
5034
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005035 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02005036 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02005037 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02005038 if (do_estack_push)
5039 estack_pop();
5040
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005041 ga_clear_strings(&lines_to_free);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005042 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005043 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02005044 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005045}
5046
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005047 void
5048set_function_type(ufunc_T *ufunc)
5049{
5050 int varargs = ufunc->uf_va_name != NULL;
5051 int argcount = ufunc->uf_args.ga_len;
5052
5053 // Create a type for the function, with the return type and any
5054 // argument types.
5055 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
5056 // The type is included in "tt_args".
5057 if (argcount > 0 || varargs)
5058 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01005059 if (ufunc->uf_type_list.ga_itemsize == 0)
5060 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005061 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
5062 argcount, &ufunc->uf_type_list);
5063 // Add argument types to the function type.
5064 if (func_type_add_arg_types(ufunc->uf_func_type,
5065 argcount + varargs,
5066 &ufunc->uf_type_list) == FAIL)
5067 return;
5068 ufunc->uf_func_type->tt_argcount = argcount + varargs;
5069 ufunc->uf_func_type->tt_min_argcount =
5070 argcount - ufunc->uf_def_args.ga_len;
5071 if (ufunc->uf_arg_types == NULL)
5072 {
5073 int i;
5074
5075 // lambda does not have argument types.
5076 for (i = 0; i < argcount; ++i)
5077 ufunc->uf_func_type->tt_args[i] = &t_any;
5078 }
5079 else
5080 mch_memmove(ufunc->uf_func_type->tt_args,
5081 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
5082 if (varargs)
5083 {
5084 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +02005085 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005086 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
5087 }
5088 }
5089 else
5090 // No arguments, can use a predefined type.
5091 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
5092 argcount, &ufunc->uf_type_list);
5093}
5094
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005095/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01005096 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01005097 */
5098 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005099delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01005100{
5101 int idx;
5102
Bram Moolenaard505d172022-12-18 21:42:55 +00005103 // In same cases the instructions may refer to a class in which the
5104 // function is defined and unreferencing the class may call back here
5105 // recursively. Set the df_delete_busy to avoid problems.
5106 if (dfunc->df_delete_busy)
5107 return;
5108 dfunc->df_delete_busy = TRUE;
5109
Bram Moolenaar20431c92020-03-20 18:39:46 +01005110 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005111 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005112
5113 if (dfunc->df_instr != NULL)
5114 {
5115 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
5116 delete_instr(dfunc->df_instr + idx);
5117 VIM_CLEAR(dfunc->df_instr);
5118 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +02005119 if (dfunc->df_instr_debug != NULL)
5120 {
5121 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
5122 delete_instr(dfunc->df_instr_debug + idx);
5123 VIM_CLEAR(dfunc->df_instr_debug);
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +02005124 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +01005125#ifdef FEAT_PROFILE
5126 if (dfunc->df_instr_prof != NULL)
5127 {
5128 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
5129 delete_instr(dfunc->df_instr_prof + idx);
5130 VIM_CLEAR(dfunc->df_instr_prof);
Bram Moolenaarc05fe072021-01-24 21:30:48 +01005131 }
5132#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +01005133
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005134 if (mark_deleted)
5135 dfunc->df_deleted = TRUE;
5136 if (dfunc->df_ufunc != NULL)
5137 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaard505d172022-12-18 21:42:55 +00005138
5139 dfunc->df_delete_busy = FALSE;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005140}
5141
5142/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005143 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01005144 * function, unless another user function still uses it.
5145 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005146 */
5147 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01005148unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005149{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00005150 if (ufunc->uf_dfunc_idx <= 0)
5151 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005152
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00005153 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5154 + ufunc->uf_dfunc_idx;
5155
5156 if (--dfunc->df_refcount <= 0)
5157 delete_def_function_contents(dfunc, TRUE);
5158 ufunc->uf_def_status = UF_NOT_COMPILED;
5159 ufunc->uf_dfunc_idx = 0;
5160 if (dfunc->df_ufunc == ufunc)
5161 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005162}
5163
Bram Moolenaarfdeab652020-09-19 15:16:50 +02005164/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01005165 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02005166 */
5167 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01005168link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02005169{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00005170 if (ufunc->uf_dfunc_idx <= 0)
5171 return;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02005172
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00005173 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5174 + ufunc->uf_dfunc_idx;
5175
5176 ++dfunc->df_refcount;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02005177}
5178
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005179#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01005180/*
5181 * Free all functions defined with ":def".
5182 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005183 void
5184free_def_functions(void)
5185{
Bram Moolenaar20431c92020-03-20 18:39:46 +01005186 int idx;
5187
5188 for (idx = 0; idx < def_functions.ga_len; ++idx)
5189 {
5190 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
5191
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005192 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01005193 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005194 }
5195
5196 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005197}
5198#endif
5199
5200
5201#endif // FEAT_EVAL