blob: 0a4c3facb689c16af808b4a46cb7b8cf191144c7 [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 Moolenaar8e7d6222020-12-18 19:49:56 +0100291 * Return TRUE if the script context is Vim9 script.
Bram Moolenaar84367732020-08-23 15:21:55 +0200292 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000293 int
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +0000294script_is_vim9(void)
Bram Moolenaar84367732020-08-23 15:21:55 +0200295{
296 return SCRIPT_ITEM(current_sctx.sc_sid)->sn_version == SCRIPT_VERSION_VIM9;
297}
298
299/*
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200300 * Lookup a variable (without s: prefix) in the current script.
Bram Moolenaar7a3b8022022-02-14 19:53:03 +0000301 * "cctx" is NULL at the script level, "cstack" is NULL in a function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100302 * Returns OK or FAIL.
303 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000304 int
Bram Moolenaardce24412022-02-08 20:35:30 +0000305script_var_exists(char_u *name, size_t len, cctx_T *cctx, cstack_T *cstack)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100306{
Bram Moolenaar9c4f5522020-09-25 21:47:28 +0200307 if (current_sctx.sc_sid <= 0)
308 return FAIL;
Bram Moolenaar15e5e532021-04-07 21:21:13 +0200309 if (script_is_vim9())
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200310 {
311 // Check script variables that were visible where the function was
312 // defined.
Bram Moolenaardce24412022-02-08 20:35:30 +0000313 if (find_script_var(name, len, cctx, cstack) != NULL)
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200314 return OK;
315 }
316 else
317 {
318 hashtab_T *ht = &SCRIPT_VARS(current_sctx.sc_sid);
319 dictitem_T *di;
320 int cc;
321
322 // Check script variables that are currently visible
323 cc = name[len];
324 name[len] = NUL;
325 di = find_var_in_ht(ht, 0, name, TRUE);
326 name[len] = cc;
327 if (di != NULL)
328 return OK;
329 }
330
331 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100332}
333
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100334/*
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200335 * Returns the index of a class method or class variable with name "name"
336 * accessible in the currently compiled function.
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200337 * If "cl_ret" is not NULL set it to the class.
338 * Otherwise return -1.
339 */
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200340 static int
341cctx_class_midx(
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200342 cctx_T *cctx,
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200343 int is_method,
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200344 char_u *name,
345 size_t len,
346 class_T **cl_ret)
347{
348 if (cctx == NULL || cctx->ctx_ufunc == NULL
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200349 || cctx->ctx_ufunc->uf_class == NULL
350 || cctx->ctx_ufunc->uf_defclass == NULL)
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200351 return -1;
352
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200353 // Search for the class method or variable in the class where the calling
354 // function is defined.
355 class_T *cl = cctx->ctx_ufunc->uf_defclass;
356 int m_idx = is_method ? class_method_idx(cl, name, len)
357 : class_member_idx(cl, name, len);
358 if (m_idx < 0)
359 {
360 cl = cl->class_extends;
361 while (cl != NULL)
362 {
363 m_idx = is_method ? class_method_idx(cl, name, len)
364 : class_member_idx(cl, name, len);
365 if (m_idx >= 0)
366 break;
367 cl = cl->class_extends;
368 }
369 }
370
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200371 if (m_idx >= 0)
372 {
373 if (cl_ret != NULL)
374 *cl_ret = cl;
375 }
376
377 return m_idx;
378}
379
380/*
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200381 * Returns the index of a class method with name "name" accessible in the
382 * currently compiled function. Returns -1 if not found. The class where the
383 * method is defined is returned in "cl_ret".
384 */
385 int
386cctx_class_method_idx(
387 cctx_T *cctx,
388 char_u *name,
389 size_t len,
390 class_T **cl_ret)
391{
392 return cctx_class_midx(cctx, TRUE, name, len, cl_ret);
393}
394
395/*
396 * Returns the index of a class variable with name "name" accessible in the
397 * currently compiled function. Returns -1 if not found. The class where the
398 * variable is defined is returned in "cl_ret".
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200399 */
400 int
401cctx_class_member_idx(
402 cctx_T *cctx,
403 char_u *name,
404 size_t len,
405 class_T **cl_ret)
406{
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +0200407 return cctx_class_midx(cctx, FALSE, name, len, cl_ret);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200408}
409
410/*
Bram Moolenaare0890d62021-02-17 14:52:14 +0100411 * Return TRUE if "name" is a local variable, argument, script variable or
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +0000412 * imported. Also if "name" is "this" and in a class method.
Bram Moolenaare0890d62021-02-17 14:52:14 +0100413 */
414 static int
415variable_exists(char_u *name, size_t len, cctx_T *cctx)
416{
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100417 return (cctx != NULL
418 && (lookup_local(name, len, NULL, cctx) == OK
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000419 || arg_exists(name, len, NULL, NULL, NULL, cctx) == OK
420 || (len == 4
421 && cctx->ctx_ufunc != NULL
Bram Moolenaar574950d2023-01-03 19:08:50 +0000422 && (cctx->ctx_ufunc->uf_flags & (FC_OBJECT|FC_NEW))
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000423 && STRNCMP(name, "this", 4) == 0)))
Bram Moolenaardce24412022-02-08 20:35:30 +0000424 || script_var_exists(name, len, cctx, NULL) == OK
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200425 || cctx_class_member_idx(cctx, name, len, NULL) >= 0
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000426 || find_imported(name, len, FALSE) != NULL;
Bram Moolenaare0890d62021-02-17 14:52:14 +0100427}
428
429/*
Bram Moolenaar6914e872021-03-06 21:01:09 +0100430 * Return TRUE if "name" is a local variable, argument, script variable,
Bram Moolenaar97f8c102022-04-02 19:43:57 +0100431 * imported or function. Or commands are being skipped, a declaration may have
432 * been skipped then.
Bram Moolenaar6914e872021-03-06 21:01:09 +0100433 */
434 static int
Bram Moolenaar77b10ff2021-03-14 13:21:35 +0100435item_exists(char_u *name, size_t len, int cmd UNUSED, cctx_T *cctx)
Bram Moolenaar6914e872021-03-06 21:01:09 +0100436{
Bram Moolenaar0631bb42022-02-13 21:20:21 +0000437 return variable_exists(name, len, cctx);
Bram Moolenaar6914e872021-03-06 21:01:09 +0100438}
439
440/*
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100441 * Check if "p[len]" is already defined, either in script "import_sid" or in
Bram Moolenaar7a3b8022022-02-14 19:53:03 +0000442 * compilation context "cctx".
443 * "cctx" is NULL at the script level, "cstack" is NULL in a function.
Bram Moolenaar0f769812020-09-12 18:32:34 +0200444 * Does not check the global namespace.
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100445 * If "is_arg" is TRUE the error message is for an argument name.
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100446 * Return FAIL and give an error if it defined.
447 */
448 int
Bram Moolenaardce24412022-02-08 20:35:30 +0000449check_defined(
450 char_u *p,
451 size_t len,
452 cctx_T *cctx,
453 cstack_T *cstack,
454 int is_arg)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100455{
Bram Moolenaar0f769812020-09-12 18:32:34 +0200456 int c = p[len];
457 ufunc_T *ufunc = NULL;
Bram Moolenaarad486a02020-08-01 23:22:18 +0200458
Bram Moolenaarda479c72021-04-10 21:01:38 +0200459 // underscore argument is OK
460 if (len == 1 && *p == '_')
461 return OK;
462
Bram Moolenaardce24412022-02-08 20:35:30 +0000463 if (script_var_exists(p, len, cctx, cstack) == OK)
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100464 {
465 if (is_arg)
466 semsg(_(e_argument_already_declared_in_script_str), p);
467 else
468 semsg(_(e_variable_already_declared_in_script_str), p);
469 return FAIL;
470 }
471
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +0200472 if (cctx_class_member_idx(cctx, p, len, NULL) >= 0)
Bram Moolenaar6acf7572023-01-01 19:53:30 +0000473 {
474 if (is_arg)
475 semsg(_(e_argument_already_declared_in_class_str), p);
476 else
477 semsg(_(e_variable_already_declared_in_class_str), p);
478 return FAIL;
479 }
480
Bram Moolenaarad486a02020-08-01 23:22:18 +0200481 p[len] = NUL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100482 if ((cctx != NULL
Bram Moolenaar709664c2020-12-12 14:33:41 +0100483 && (lookup_local(p, len, NULL, cctx) == OK
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200484 || arg_exists(p, len, NULL, NULL, NULL, cctx) == OK))
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000485 || find_imported(p, len, FALSE) != NULL
Bram Moolenaaracc4b562022-01-24 13:54:45 +0000486 || (ufunc = find_func_even_dead(p, 0)) != NULL)
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100487 {
Bram Moolenaar0f769812020-09-12 18:32:34 +0200488 // A local or script-local function can shadow a global function.
Bram Moolenaar577dc932021-06-27 15:35:40 +0200489 if (ufunc == NULL || ((ufunc->uf_flags & FC_DEAD) == 0
490 && (!func_is_global(ufunc)
491 || (p[0] == 'g' && p[1] == ':'))))
Bram Moolenaar0f769812020-09-12 18:32:34 +0200492 {
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100493 if (is_arg)
494 semsg(_(e_argument_name_shadows_existing_variable_str), p);
495 else
496 semsg(_(e_name_already_defined_str), p);
Bram Moolenaar0f769812020-09-12 18:32:34 +0200497 p[len] = c;
Bram Moolenaar0f769812020-09-12 18:32:34 +0200498 return FAIL;
499 }
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100500 }
Bram Moolenaarad486a02020-08-01 23:22:18 +0200501 p[len] = c;
Bram Moolenaar5269bd22020-03-09 19:25:27 +0100502 return OK;
503}
504
Bram Moolenaar65b95452020-07-19 14:03:09 +0200505
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100506/*
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200507 * Return TRUE if "actual" could be "expected" and a runtime typecheck is to be
508 * used. Return FALSE if the types will never match.
509 */
Yegappan Lakshmanan8ee52af2021-08-09 19:59:06 +0200510 static int
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200511use_typecheck(type_T *actual, type_T *expected)
512{
513 if (actual->tt_type == VAR_ANY
514 || actual->tt_type == VAR_UNKNOWN
515 || (actual->tt_type == VAR_FUNC
516 && (expected->tt_type == VAR_FUNC
517 || expected->tt_type == VAR_PARTIAL)
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000518 && (actual->tt_member == &t_any
519 || actual->tt_member == &t_unknown
520 || actual->tt_argcount < 0)
521 && (actual->tt_member == &t_unknown ||
522 (actual->tt_member == &t_void)
Bram Moolenaar328eac22021-01-07 19:23:08 +0100523 == (expected->tt_member == &t_void))))
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200524 return TRUE;
LemonBoy50d48542024-07-04 17:03:17 +0200525 if (actual->tt_type == VAR_OBJECT && expected->tt_type == VAR_OBJECT)
526 return TRUE;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100527 if ((actual->tt_type == VAR_LIST
528 || actual->tt_type == VAR_TUPLE
529 || actual->tt_type == VAR_DICT)
530 && actual->tt_type == expected->tt_type)
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200531 // This takes care of a nested list or dict.
532 return use_typecheck(actual->tt_member, expected->tt_member);
533 return FALSE;
534}
535
536/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200537 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200538 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200539 * - "actual" is a type that can be "expected" type: add a runtime check; or
540 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200541 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
542 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200543 */
Bram Moolenaara1c51952022-02-02 16:20:26 +0000544 int
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200545need_type_where(
546 type_T *actual,
547 type_T *expected,
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000548 int number_ok, // expect VAR_FLOAT but VAR_NUMBER is OK
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200549 int offset,
550 where_T where,
551 cctx_T *cctx,
552 int silent,
553 int actual_is_const)
554{
Bram Moolenaar44a89772021-12-18 12:31:33 +0000555 int ret;
556
Ernie Raele75fde62023-12-21 17:18:54 +0100557 if (expected->tt_type != VAR_CLASS && expected->tt_type != VAR_TYPEALIAS)
558 {
559 if (check_type_is_value(actual) == FAIL)
560 return FAIL;
561 }
562
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200563 if (expected == &t_bool && actual != &t_bool
564 && (actual->tt_flags & TTFLAG_BOOL_OK))
565 {
566 // Using "0", "1" or the result of an expression with "&&" or "||" as a
567 // boolean is OK but requires a conversion.
568 generate_2BOOL(cctx, FALSE, offset);
569 return OK;
570 }
571
Bram Moolenaar44a89772021-12-18 12:31:33 +0000572 ret = check_type_maybe(expected, actual, FALSE, where);
573 if (ret == OK)
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200574 return OK;
575
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000576 // If actual a constant a runtime check makes no sense. If it's
577 // null_function it is OK.
578 if (actual_is_const && ret == MAYBE && actual == &t_func_unknown)
579 return OK;
580
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200581 // If the actual type can be the expected type add a runtime check.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000582 if (!actual_is_const && ret == MAYBE && use_typecheck(actual, expected))
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200583 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000584 generate_TYPECHECK(cctx, expected, number_ok, offset,
LemonBoyc5d27442023-08-19 13:02:35 +0200585 where.wt_kind == WT_VARIABLE, where.wt_index);
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200586 return OK;
587 }
588
589 if (!silent)
590 type_mismatch_where(expected, actual, where);
591 return FAIL;
592}
593
Bram Moolenaar351ead02021-01-16 16:07:01 +0100594 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200595need_type(
596 type_T *actual,
597 type_T *expected,
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000598 int number_ok, // when expected is float number is also OK
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200599 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +0100600 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200601 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200602 int silent,
603 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200604{
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200605 where_T where = WHERE_INIT;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100606
LemonBoyc5d27442023-08-19 13:02:35 +0200607 if (arg_idx > 0)
608 {
609 where.wt_index = arg_idx;
610 where.wt_kind = WT_ARGUMENT;
611 }
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000612 return need_type_where(actual, expected, number_ok, offset, where,
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200613 cctx, silent, actual_is_const);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200614}
615
616/*
Bram Moolenaarfa103972022-09-29 19:14:42 +0100617 * Set type of variable "lvar" to "type". If the variable is a constant then
618 * the type gets TTFLAG_CONST.
619 */
620 static void
621set_var_type(lvar_T *lvar, type_T *type_arg, cctx_T *cctx)
622{
623 type_T *type = type_arg;
624
Bram Moolenaar6586a012022-09-30 11:04:50 +0100625 if (lvar->lv_const == ASSIGN_CONST && (type->tt_flags & TTFLAG_CONST) == 0)
Bram Moolenaarfa103972022-09-29 19:14:42 +0100626 {
627 if (type->tt_flags & TTFLAG_STATIC)
628 // entry in static_types[] is followed by const type
629 type = type + 1;
630 else
631 {
632 type = copy_type(type, cctx->ctx_type_list);
633 type->tt_flags |= TTFLAG_CONST;
634 }
635 }
636 lvar->lv_type = type;
637}
638
639/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100640 * Reserve space for a local variable.
Bram Moolenaar6586a012022-09-30 11:04:50 +0100641 * "assign" can be ASSIGN_VAR for :var, ASSIGN_CONST for :const and
642 * ASSIGN_FINAL for :final.
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200643 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100644 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000645 lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +0200646reserve_local(
647 cctx_T *cctx,
648 char_u *name,
649 size_t len,
Bram Moolenaar6586a012022-09-30 11:04:50 +0100650 int assign,
Bram Moolenaare8211a32020-10-09 22:04:29 +0200651 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100652{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100653 lvar_T *lvar;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200654 dfunc_T *dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100655
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200656 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100657 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200658 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200659 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100660 }
661
Bram Moolenaar35578162021-08-02 19:10:38 +0200662 if (GA_GROW_FAILS(&cctx->ctx_locals, 1))
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200663 return NULL;
664 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +0200665 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100666
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200667 // Every local variable uses the next entry on the stack. We could re-use
668 // the last ones when leaving a scope, but then variables used in a closure
669 // might get overwritten. To keep things simple do not re-use stack
670 // entries. This is less efficient, but memory is cheap these days.
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200671 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx;
672 lvar->lv_idx = dfunc->df_var_names.ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200673
Bram Moolenaar71ccd032020-06-12 22:59:11 +0200674 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar6586a012022-09-30 11:04:50 +0100675 lvar->lv_const = assign;
Bram Moolenaarfa103972022-09-29 19:14:42 +0100676 if (type == &t_unknown || type == &t_any)
677 // type not known yet, may be inferred from RHS
678 lvar->lv_type = type;
679 else
680 // may use TTFLAG_CONST
681 set_var_type(lvar, type, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100682
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200683 // Remember the name for debugging.
Bram Moolenaar35578162021-08-02 19:10:38 +0200684 if (GA_GROW_FAILS(&dfunc->df_var_names, 1))
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200685 return NULL;
686 ((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] =
687 vim_strsave(lvar->lv_name);
688 ++dfunc->df_var_names.ga_len;
689
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200690 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100691}
692
693/*
Bram Moolenaar08251752021-01-11 21:20:18 +0100694 * If "check_writable" is ASSIGN_CONST give an error if the variable was
695 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
696 * error if the variable was defined with :const.
697 */
698 static int
699check_item_writable(svar_T *sv, int check_writable, char_u *name)
700{
701 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
702 || (check_writable == ASSIGN_FINAL
703 && sv->sv_const == ASSIGN_CONST))
704 {
Bram Moolenaard8e44472021-07-21 22:20:33 +0200705 semsg(_(e_cannot_change_readonly_variable_str), name);
Bram Moolenaar08251752021-01-11 21:20:18 +0100706 return FAIL;
707 }
708 return OK;
709}
710
711/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100712 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +0100713 * Pass "check_writable" to check_item_writable().
Bram Moolenaar7a3b8022022-02-14 19:53:03 +0000714 * "cctx" is NULL at the script level, "cstack" is NULL in a function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100715 * Returns the index in "sn_var_vals" if found.
716 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +0100717 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100718 */
719 int
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000720get_script_item_idx(
721 int sid,
722 char_u *name,
723 int check_writable,
724 cctx_T *cctx,
725 cstack_T *cstack)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100726{
727 hashtab_T *ht;
728 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100729 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200730 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100731 int idx;
732
Bram Moolenaare3d46852020-08-29 13:39:17 +0200733 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100734 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200735 if (sid == current_sctx.sc_sid)
736 {
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000737 sallvar_T *sav = find_script_var(name, 0, cctx, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200738
739 if (sav == NULL)
740 return -2;
741 idx = sav->sav_var_vals_idx;
742 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +0100743 if (check_item_writable(sv, check_writable, name) == FAIL)
744 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200745 return idx;
746 }
747
748 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100749 ht = &SCRIPT_VARS(sid);
750 di = find_var_in_ht(ht, 0, name, TRUE);
751 if (di == NULL)
Bram Moolenaar577bd852022-01-10 18:42:52 +0000752 {
753 if (si->sn_autoload_prefix != NULL)
754 {
755 hashitem_T *hi;
756
757 // A variable exported from an autoload script is in the global
758 // variables, we can find it in the all_vars table.
759 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
760 if (!HASHITEM_EMPTY(hi))
761 return HI2SAV(hi)->sav_var_vals_idx;
762 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100763 return -2;
Bram Moolenaar577bd852022-01-10 18:42:52 +0000764 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100765
766 // Now find the svar_T index in sn_var_vals.
767 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
768 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200769 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100770 if (sv->sv_tv == &di->di_tv)
771 {
Bram Moolenaar08251752021-01-11 21:20:18 +0100772 if (check_item_writable(sv, check_writable, name) == FAIL)
773 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100774 return idx;
775 }
776 }
777 return -1;
778}
779
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000780 static imported_T *
781find_imported_in_script(char_u *name, size_t len, int sid)
782{
783 scriptitem_T *si;
784 int idx;
785
786 if (!SCRIPT_ID_VALID(sid))
787 return NULL;
788 si = SCRIPT_ITEM(sid);
789 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
790 {
791 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
792
793 if (len == 0 ? STRCMP(name, import->imp_name) == 0
794 : STRLEN(import->imp_name) == len
795 && STRNCMP(name, import->imp_name, len) == 0)
796 return import;
797 }
798 return NULL;
799}
800
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100801/*
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000802 * Find "name" in imported items of the current script.
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100803 * If "len" is 0 use any length that works.
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000804 * If "load" is TRUE and the script was not loaded yet, load it now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100805 */
806 imported_T *
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000807find_imported(char_u *name, size_t len, int load)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100808{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200809 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +0200810 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100811
Bram Moolenaarb775e722022-11-22 18:12:44 +0000812 // Skip over "s:" before "s:something" to find the import name.
813 int off = name[0] == 's' && name[1] == ':' ? 2 : 0;
814
815 imported_T *ret = find_imported_in_script(name + off, len - off,
816 current_sctx.sc_sid);
Bram Moolenaarb697dc22022-01-22 11:27:29 +0000817 if (ret != NULL && load && (ret->imp_flags & IMP_FLAGS_AUTOLOAD))
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000818 {
Bram Moolenaar753885b2022-08-24 16:30:36 +0100819 scid_T actual_sid = 0;
Bram Moolenaar6809ff92022-07-26 15:10:56 +0100820 int save_emsg_off = emsg_off;
821
822 // "emsg_off" will be set when evaluating an expression silently, but
823 // we do want to know about errors in a script. Also because it then
824 // aborts when an error is encountered.
825 emsg_off = FALSE;
Bram Moolenaar17d36cb2022-01-12 11:46:40 +0000826
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000827 // script found before but not loaded yet
Bram Moolenaarb697dc22022-01-22 11:27:29 +0000828 ret->imp_flags &= ~IMP_FLAGS_AUTOLOAD;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000829 (void)do_source(SCRIPT_ITEM(ret->imp_sid)->sn_name, FALSE,
Bram Moolenaar753885b2022-08-24 16:30:36 +0100830 DOSO_NONE, &actual_sid);
831 // If the script is a symlink it may be sourced with another name, may
832 // need to adjust the script ID for that.
833 if (actual_sid != 0)
834 ret->imp_sid = actual_sid;
Bram Moolenaar6809ff92022-07-26 15:10:56 +0100835
836 emsg_off = save_emsg_off;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000837 }
838 return ret;
Bram Moolenaarefa94442020-08-08 22:16:00 +0200839}
840
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100841/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000842 * Called when checking for a following operator at "arg". When the rest of
843 * the line is empty or only a comment, peek the next line. If there is a next
844 * line return a pointer to it and set "nextp".
845 * Otherwise skip over white space.
846 */
847 char_u *
848may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
849{
850 char_u *p = skipwhite(arg);
851
852 *nextp = NULL;
853 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
854 {
855 *nextp = peek_next_line_from_context(cctx);
856 if (*nextp != NULL)
857 return *nextp;
858 }
859 return p;
860}
861
862/*
Bram Moolenaar23c55272020-06-21 16:58:13 +0200863 * Return a pointer to the next line that isn't empty or only contains a
864 * comment. Skips over white space.
865 * Returns NULL if there is none.
866 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200867 char_u *
868peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +0200869{
870 int lnum = cctx->ctx_lnum;
871
872 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
873 {
874 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +0200875 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +0200876
Bram Moolenaarba60cc42020-08-12 19:15:33 +0200877 // ignore NULLs inserted for continuation lines
878 if (line != NULL)
879 {
880 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100881 if (vim9_bad_comment(p))
882 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +0200883 if (*p != NUL && !vim9_comment_start(p))
884 return p;
885 }
Bram Moolenaar23c55272020-06-21 16:58:13 +0200886 }
887 return NULL;
888}
889
890/*
Bram Moolenaare6085c52020-04-12 20:19:16 +0200891 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +0200892 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +0200893 * Returns NULL when at the end.
894 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200895 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +0200896next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +0200897{
Bram Moolenaar7a092242020-04-16 22:10:49 +0200898 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +0200899
900 do
901 {
902 ++cctx->ctx_lnum;
903 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +0200904 {
905 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +0200906 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +0200907 }
Bram Moolenaare6085c52020-04-12 20:19:16 +0200908 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +0200909 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200910 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +0200911 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +0200912 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +0200913 return line;
914}
915
916/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +0100917 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200918 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +0200919 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200920 * Return FAIL if beyond the last line, "*arg" is unmodified then.
921 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000922 int
Bram Moolenaar2c330432020-04-13 14:41:35 +0200923may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200924{
Bram Moolenaar5afd0812021-01-03 18:33:13 +0100925 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100926 if (vim9_bad_comment(*arg))
927 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200928 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200929 {
Bram Moolenaar23c55272020-06-21 16:58:13 +0200930 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200931
932 if (next == NULL)
933 return FAIL;
934 *arg = skipwhite(next);
935 }
936 return OK;
937}
938
Bram Moolenaara7eedf32020-07-10 21:50:41 +0200939/*
940 * Idem, and give an error when failed.
941 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000942 int
Bram Moolenaara7eedf32020-07-10 21:50:41 +0200943may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
944{
945 if (may_get_next_line(whitep, arg, cctx) == FAIL)
946 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +0100947 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200948 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +0200949 return FAIL;
950 }
951 return OK;
952}
953
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100954/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200955 * Get a line from the compilation context, compatible with exarg_T getline().
956 * Return a pointer to the line in allocated memory.
957 * Return NULL for end-of-file or some error.
958 */
959 static char_u *
960exarg_getline(
961 int c UNUSED,
962 void *cookie,
963 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200964 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +0200965{
966 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +0200967 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +0200968
Bram Moolenaar66250c92020-08-20 15:02:42 +0200969 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +0200970 {
Bram Moolenaar2914a202020-09-27 18:24:03 +0200971 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +0200972 return NULL;
973 ++cctx->ctx_lnum;
974 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
975 // Comment lines result in NULL pointers, skip them.
976 if (p != NULL)
977 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +0200978 }
Bram Moolenaar04b12692020-05-04 23:24:44 +0200979}
980
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100981 void
982fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
983{
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100984 eap->ea_getline = exarg_getline;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100985 eap->cookie = cctx;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +0000986 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100987}
988
Bram Moolenaar04b12692020-05-04 23:24:44 +0200989/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000990 * Return TRUE if "ufunc" should be compiled, taking into account whether
991 * "profile" indicates profiling is to be done.
992 */
993 int
994func_needs_compiling(ufunc_T *ufunc, compiletype_T compile_type)
995{
996 switch (ufunc->uf_def_status)
997 {
998 case UF_TO_BE_COMPILED:
999 return TRUE;
1000
1001 case UF_COMPILED:
1002 {
1003 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1004 + ufunc->uf_dfunc_idx;
1005
1006 switch (compile_type)
1007 {
1008 case CT_PROFILE:
1009#ifdef FEAT_PROFILE
1010 return dfunc->df_instr_prof == NULL;
1011#endif
1012 case CT_NONE:
1013 return dfunc->df_instr == NULL;
1014 case CT_DEBUG:
1015 return dfunc->df_instr_debug == NULL;
1016 }
1017 }
1018
1019 case UF_NOT_COMPILED:
1020 case UF_COMPILE_ERROR:
1021 case UF_COMPILING:
1022 break;
1023 }
1024 return FALSE;
1025}
1026
1027/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02001028 * Compile a nested :def command.
1029 */
1030 static char_u *
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001031compile_nested_function(exarg_T *eap, cctx_T *cctx, garray_T *lines_to_free)
Bram Moolenaar04b12692020-05-04 23:24:44 +02001032{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001033 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02001034 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02001035 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001036 int off;
1037 char_u *func_name;
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +01001038 string_T lambda_name;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001039 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001040 int r = FAIL;
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02001041 compiletype_T compile_type;
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001042 int funcref_isn_idx = -1;
Bram Moolenaar1889f492022-08-16 19:34:44 +01001043 lvar_T *lvar = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001044
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001045 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02001046 {
1047 emsg(_(e_cannot_use_bang_with_nested_def));
1048 return NULL;
1049 }
1050
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001051 if (*name_start == '/')
1052 {
1053 name_end = skip_regexp(name_start + 1, '/', TRUE);
1054 if (*name_end == '/')
1055 ++name_end;
Bram Moolenaar63b91732021-08-05 20:40:03 +02001056 set_nextcmd(eap, name_end);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001057 }
1058 if (name_end == name_start || *skipwhite(name_end) != '(')
1059 {
1060 if (!ends_excmd2(name_start, name_end))
1061 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00001062 if (*skipwhite(name_end) == '.')
1063 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
1064 eap->cmd);
1065 else
1066 semsg(_(e_invalid_command_str), eap->cmd);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001067 return NULL;
1068 }
1069
1070 // "def" or "def Name": list functions
1071 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
1072 return NULL;
1073 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
1074 }
1075
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02001076 // Only g:Func() can use a namespace.
1077 if (name_start[1] == ':' && !is_global)
1078 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001079 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02001080 return NULL;
1081 }
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00001082 if (cctx->ctx_skip != SKIP_YES
1083 && check_defined(name_start, name_end - name_start, cctx,
Bram Moolenaardce24412022-02-08 20:35:30 +00001084 NULL, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02001085 return NULL;
Bram Moolenaarf681cfb2022-02-07 20:30:57 +00001086 if (!ASCII_ISUPPER(is_global ? name_start[2] : name_start[0]))
1087 {
Bram Moolenaar3787f262022-02-07 21:54:01 +00001088 semsg(_(e_function_name_must_start_with_capital_str), name_start);
Bram Moolenaarf681cfb2022-02-07 20:30:57 +00001089 return NULL;
1090 }
Bram Moolenaareef21022020-08-01 22:16:43 +02001091
Bram Moolenaar04b12692020-05-04 23:24:44 +02001092 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001093 fill_exarg_from_cctx(eap, cctx);
1094
Bram Moolenaar04b12692020-05-04 23:24:44 +02001095 eap->forceit = FALSE;
Bram Moolenaar38453522021-11-28 22:00:12 +00001096 // We use the special <Lamba>99 name, but it's not really a lambda.
John Marriottb32800f2025-02-01 15:25:34 +01001097 lambda_name = get_lambda_name();
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +01001098 lambda_name.string = vim_strnsave(lambda_name.string, lambda_name.length);
1099 if (lambda_name.string == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001100 return NULL;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001101
1102 // This may free the current line, make a copy of the name.
1103 off = is_global ? 2 : 0;
1104 func_name = vim_strnsave(name_start + off, name_end - name_start - off);
1105 if (func_name == NULL)
1106 {
1107 r = FAIL;
1108 goto theend;
1109 }
1110
Bram Moolenaar39c82ea2023-01-02 13:08:01 +00001111 // Make sure "KeyTyped" is not set, it may cause indent to be written.
1112 int save_KeyTyped = KeyTyped;
1113 KeyTyped = FALSE;
1114
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +01001115 ufunc = define_function(eap, lambda_name.string, lines_to_free, 0, NULL, 0);
Bram Moolenaar39c82ea2023-01-02 13:08:01 +00001116
1117 KeyTyped = save_KeyTyped;
1118
Bram Moolenaar822ba242020-05-24 23:00:18 +02001119 if (ufunc == NULL)
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001120 {
1121 r = eap->skip ? OK : FAIL;
1122 goto theend;
1123 }
Bram Moolenaar7473a842021-12-28 17:55:26 +00001124 if (eap->nextcmd != NULL)
1125 {
1126 semsg(_(e_text_found_after_str_str),
1127 eap->cmdidx == CMD_def ? "enddef" : "endfunction", eap->nextcmd);
1128 r = FAIL;
Bram Moolenaard2939812021-12-30 17:09:05 +00001129 func_ptr_unref(ufunc);
Bram Moolenaar7473a842021-12-28 17:55:26 +00001130 goto theend;
1131 }
Bram Moolenaar8863bda2021-03-17 18:42:08 +01001132
1133 // copy over the block scope IDs before compiling
1134 if (!is_global && cctx->ctx_ufunc->uf_block_depth > 0)
1135 {
1136 int block_depth = cctx->ctx_ufunc->uf_block_depth;
1137
1138 ufunc->uf_block_ids = ALLOC_MULT(int, block_depth);
1139 if (ufunc->uf_block_ids != NULL)
1140 {
1141 mch_memmove(ufunc->uf_block_ids, cctx->ctx_ufunc->uf_block_ids,
1142 sizeof(int) * block_depth);
1143 ufunc->uf_block_depth = block_depth;
1144 }
1145 }
1146
Bram Moolenaara915fa02022-03-23 11:29:15 +00001147 // Define the funcref before compiling, so that it is found by any
1148 // recursive call.
1149 if (is_global)
1150 {
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +01001151 r = generate_NEWFUNC(cctx, lambda_name.string, func_name);
Bram Moolenaara915fa02022-03-23 11:29:15 +00001152 func_name = NULL;
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +01001153 lambda_name.string = NULL;
1154 lambda_name.length = 0;
Bram Moolenaara915fa02022-03-23 11:29:15 +00001155 }
1156 else
1157 {
1158 // Define a local variable for the function reference.
Bram Moolenaar1889f492022-08-16 19:34:44 +01001159 lvar = reserve_local(cctx, func_name, name_end - name_start,
Bram Moolenaar6586a012022-09-30 11:04:50 +01001160 ASSIGN_CONST, ufunc->uf_func_type);
Bram Moolenaara915fa02022-03-23 11:29:15 +00001161 if (lvar == NULL)
1162 goto theend;
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001163 if (generate_FUNCREF(cctx, ufunc, NULL, FALSE, 0, &funcref_isn_idx) == FAIL)
Bram Moolenaara915fa02022-03-23 11:29:15 +00001164 goto theend;
1165 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
1166 }
1167
Bram Moolenaar139575d2022-03-15 19:29:30 +00001168 compile_type = get_compile_type(ufunc);
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02001169#ifdef FEAT_PROFILE
1170 // If the outer function is profiled, also compile the nested function for
1171 // profiling.
1172 if (cctx->ctx_compile_type == CT_PROFILE)
1173 compile_type = CT_PROFILE;
1174#endif
1175 if (func_needs_compiling(ufunc, compile_type)
1176 && compile_def_function(ufunc, TRUE, compile_type, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02001177 {
1178 func_ptr_unref(ufunc);
Bram Moolenaar1889f492022-08-16 19:34:44 +01001179 if (lvar != NULL)
1180 // Now the local variable can't be used.
1181 *lvar->lv_name = '/'; // impossible value
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001182 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02001183 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02001184
Bram Moolenaar648594e2021-07-11 17:55:01 +02001185#ifdef FEAT_PROFILE
1186 // When the outer function is compiled for profiling, the nested function
1187 // may be called without profiling. Compile it here in the right context.
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02001188 if (compile_type == CT_PROFILE && func_needs_compiling(ufunc, CT_NONE))
Bram Moolenaar648594e2021-07-11 17:55:01 +02001189 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1190#endif
1191
Bram Moolenaara915fa02022-03-23 11:29:15 +00001192 // If a FUNCREF instruction was generated, set the index after compiling.
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001193 if (funcref_isn_idx != -1 && ufunc->uf_def_status == UF_COMPILED)
1194 {
1195 isn_T *funcref_isn = ((isn_T *)cctx->ctx_instr.ga_data) +
1196 funcref_isn_idx;
Bram Moolenaara915fa02022-03-23 11:29:15 +00001197 funcref_isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001198 }
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001199
1200theend:
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +01001201 vim_free(lambda_name.string);
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001202 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001203 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02001204}
1205
1206/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001207 * Compile one Vim expression {expr} in string "p".
1208 * "p" points to the opening "{".
1209 * Return a pointer to the character after "}", NULL for an error.
1210 */
1211 char_u *
1212compile_one_expr_in_str(char_u *p, cctx_T *cctx)
1213{
1214 char_u *block_start;
1215 char_u *block_end;
1216
1217 // Skip the opening {.
1218 block_start = skipwhite(p + 1);
1219 block_end = block_start;
1220 if (*block_start != NUL && skip_expr(&block_end, NULL) == FAIL)
1221 return NULL;
1222 block_end = skipwhite(block_end);
1223 // The block must be closed by a }.
1224 if (*block_end != '}')
1225 {
1226 semsg(_(e_missing_close_curly_str), p);
1227 return NULL;
1228 }
1229 if (compile_expr0(&block_start, cctx) == FAIL)
1230 return NULL;
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02001231 may_generate_2STRING(-1, TOSTRING_INTERPOLATE, cctx);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001232
1233 return block_end + 1;
1234}
1235
1236/*
LemonBoy2eaef102022-05-06 13:14:50 +01001237 * Compile a string "str" (either containing a literal string or a mix of
1238 * literal strings and Vim expressions of the form `{expr}`). This is used
1239 * when compiling a heredoc assignment to a variable or an interpolated string
1240 * in a Vim9 def function. Vim9 instructions are generated to push strings,
1241 * evaluate expressions, concatenate them and create a list of lines. When
1242 * "evalstr" is TRUE, Vim expressions in "str" are evaluated.
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001243 */
1244 int
LemonBoy2eaef102022-05-06 13:14:50 +01001245compile_all_expr_in_str(char_u *str, int evalstr, cctx_T *cctx)
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001246{
LemonBoy2eaef102022-05-06 13:14:50 +01001247 char_u *p = str;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001248 char_u *val;
LemonBoy2eaef102022-05-06 13:14:50 +01001249 int count = 0;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001250
1251 if (cctx->ctx_skip == SKIP_YES)
1252 return OK;
1253
LemonBoy2eaef102022-05-06 13:14:50 +01001254 if (!evalstr || *str == NUL)
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001255 {
LemonBoy2eaef102022-05-06 13:14:50 +01001256 // Literal string, possibly empty.
1257 val = *str != NUL ? vim_strsave(str) : NULL;
1258 return generate_PUSHS(cctx, &val);
1259 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001260
LemonBoy2eaef102022-05-06 13:14:50 +01001261 // Push all the string pieces to the stack, followed by a ISN_CONCAT.
1262 while (*p != NUL)
1263 {
1264 char_u *lit_start;
LemonBoy2eaef102022-05-06 13:14:50 +01001265 int escaped_brace = FALSE;
1266
1267 // Look for a block start.
1268 lit_start = p;
1269 while (*p != '{' && *p != '}' && *p != NUL)
1270 ++p;
1271
1272 if (*p != NUL && *p == p[1])
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001273 {
LemonBoy2eaef102022-05-06 13:14:50 +01001274 // Escaped brace, unescape and continue.
1275 // Include the brace in the literal string.
1276 ++p;
1277 escaped_brace = TRUE;
1278 }
1279 else if (*p == '}')
1280 {
1281 semsg(_(e_stray_closing_curly_str), str);
1282 return FAIL;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001283 }
LemonBoy372bcce2022-04-25 12:43:20 +01001284
LemonBoy2eaef102022-05-06 13:14:50 +01001285 // Append the literal part.
1286 if (p != lit_start)
1287 {
1288 val = vim_strnsave(lit_start, (size_t)(p - lit_start));
1289 if (generate_PUSHS(cctx, &val) == FAIL)
1290 return FAIL;
1291 ++count;
1292 }
1293
1294 if (*p == NUL)
1295 break;
1296
1297 if (escaped_brace)
1298 {
1299 // Skip the second brace.
1300 ++p;
1301 continue;
1302 }
1303
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001304 p = compile_one_expr_in_str(p, cctx);
1305 if (p == NULL)
LemonBoy2eaef102022-05-06 13:14:50 +01001306 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01001307 ++count;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001308 }
LemonBoy2eaef102022-05-06 13:14:50 +01001309
1310 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001311 if (count > 1)
LemonBoy2eaef102022-05-06 13:14:50 +01001312 return generate_CONCAT(cctx, count);
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001313
1314 return OK;
1315}
1316
1317/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001318 * Return the length of an assignment operator, or zero if there isn't one.
1319 */
1320 int
1321assignment_len(char_u *p, int *heredoc)
1322{
1323 if (*p == '=')
1324 {
1325 if (p[1] == '<' && p[2] == '<')
1326 {
1327 *heredoc = TRUE;
1328 return 3;
1329 }
1330 return 1;
1331 }
1332 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
1333 return 2;
1334 if (STRNCMP(p, "..=", 3) == 0)
1335 return 3;
1336 return 0;
1337}
1338
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001339/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001340 * Generate the load instruction for "name".
1341 */
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001342 static int
Bram Moolenaard505d172022-12-18 21:42:55 +00001343generate_loadvar(cctx_T *cctx, lhs_T *lhs)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001344{
Bram Moolenaard505d172022-12-18 21:42:55 +00001345 char_u *name = lhs->lhs_name;
1346 type_T *type = lhs->lhs_type;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001347 int res = OK;
Bram Moolenaard505d172022-12-18 21:42:55 +00001348
1349 switch (lhs->lhs_dest)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001350 {
1351 case dest_option:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001352 case dest_func_option:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001353 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1354 break;
1355 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01001356 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001357 {
1358 if (name[2] == NUL)
1359 generate_instr_type(cctx, ISN_LOADGDICT, &t_dict_any);
1360 else
1361 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
1362 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01001363 else
1364 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001365 break;
1366 case dest_buffer:
1367 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
1368 break;
1369 case dest_window:
1370 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
1371 break;
1372 case dest_tab:
1373 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
1374 break;
1375 case dest_script:
Ernie Rael3f821d62024-04-24 20:07:50 +02001376 case dest_script_v9:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001377 res = compile_load_scriptvar(cctx,
Hirohito Higashi6c012832025-02-25 20:29:50 +01001378 name + (name[1] == ':' ? 2 : 0), NULL, NULL, NULL);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001379 break;
1380 case dest_env:
1381 // Include $ in the name here
1382 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
1383 break;
1384 case dest_reg:
1385 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
1386 break;
1387 case dest_vimvar:
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001388 generate_LOADV(cctx, name + 2);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001389 break;
1390 case dest_local:
Bram Moolenaar4875d6a2022-08-17 15:55:51 +01001391 if (cctx->ctx_skip != SKIP_YES)
1392 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001393 lvar_T *lvar = lhs->lhs_lvar;
Bram Moolenaar4875d6a2022-08-17 15:55:51 +01001394 if (lvar->lv_from_outer > 0)
1395 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
Bram Moolenaar65449bd2022-09-19 16:02:43 +01001396 lvar->lv_loop_depth, lvar->lv_loop_idx, type);
Bram Moolenaar4875d6a2022-08-17 15:55:51 +01001397 else
1398 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
1399 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001400 break;
Bram Moolenaard505d172022-12-18 21:42:55 +00001401 case dest_class_member:
1402 generate_CLASSMEMBER(cctx, TRUE, lhs->lhs_class,
1403 lhs->lhs_classmember_idx);
1404 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01001405 case dest_expr:
1406 // list or dict value should already be on the stack.
1407 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001408 }
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001409
1410 return res;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001411}
1412
Bram Moolenaardc234ca2020-11-28 18:52:33 +01001413/*
1414 * Skip over "[expr]" or ".member".
1415 * Does not check for any errors.
1416 */
1417 static char_u *
1418skip_index(char_u *start)
1419{
1420 char_u *p = start;
1421
1422 if (*p == '[')
1423 {
1424 p = skipwhite(p + 1);
1425 (void)skip_expr(&p, NULL);
1426 p = skipwhite(p);
1427 if (*p == ']')
1428 return p + 1;
1429 return p;
1430 }
1431 // if (*p == '.')
1432 return to_name_end(p + 1, TRUE);
1433}
1434
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001435 void
1436vim9_declare_error(char_u *name)
1437{
1438 char *scope = "";
1439
1440 switch (*name)
1441 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02001442 case 'g': scope = _("global"); break;
1443 case 'b': scope = _("buffer"); break;
1444 case 'w': scope = _("window"); break;
1445 case 't': scope = _("tab"); break;
1446 case 'v': scope = "v:"; break;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001447 case '$': semsg(_(e_cannot_declare_an_environment_variable_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001448 return;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001449 case '&': semsg(_(e_cannot_declare_an_option_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001450 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001451 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001452 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02001453 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001454 }
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001455 semsg(_(e_cannot_declare_a_scope_variable_str), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001456}
1457
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001458/*
Bram Moolenaar3558afe2022-10-13 16:12:57 +01001459 * Return TRUE if "name" is a valid register to use.
1460 * Return FALSE and give an error message if not.
1461 */
1462 static int
1463valid_dest_reg(int name)
1464{
1465 if ((name == '@' || valid_yank_reg(name, FALSE)) && name != '.')
1466 return TRUE;
1467 emsg_invreg(name);
1468 return FAIL;
1469}
1470
1471/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001472 * For one assignment figure out the type of destination. Return it in "dest".
1473 * When not recognized "dest" is not set.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001474 * For an option "option_scope" is set.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001475 * For a v:var "vimvaridx" is set.
1476 * "type" is set to the destination type if known, unchanted otherwise.
1477 * Return FAIL if an error message was given.
1478 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001479 int
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001480get_var_dest(
1481 char_u *name,
1482 assign_dest_T *dest,
Bram Moolenaar97f8c102022-04-02 19:43:57 +01001483 cmdidx_T cmdidx,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001484 int *option_scope,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001485 int *vimvaridx,
1486 type_T **type,
1487 cctx_T *cctx)
1488{
1489 char_u *p;
1490
1491 if (*name == '&')
1492 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001493 int cc;
1494 long numval;
1495 getoption_T opt_type;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001496 int opt_p_flags;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001497
1498 *dest = dest_option;
1499 if (cmdidx == CMD_final || cmdidx == CMD_const)
1500 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001501 emsg(_(e_cannot_lock_option));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001502 return FAIL;
1503 }
1504 p = name;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001505 p = find_option_end(&p, option_scope);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001506 if (p == NULL)
1507 {
1508 // cannot happen?
Bram Moolenaar108010a2021-06-27 22:03:33 +02001509 emsg(_(e_unexpected_characters_in_assignment));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001510 return FAIL;
1511 }
1512 cc = *p;
1513 *p = NUL;
1514 opt_type = get_option_value(skip_option_env_lead(name),
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001515 &numval, NULL, &opt_p_flags, *option_scope);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001516 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001517 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001518 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001519 case gov_unknown:
Bram Moolenaare1242042021-12-16 20:56:57 +00001520 semsg(_(e_unknown_option_str), name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001521 return FAIL;
1522 case gov_string:
1523 case gov_hidden_string:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001524 if (opt_p_flags & P_FUNC)
1525 {
1526 // might be a Funcref, check the type later
1527 *type = &t_any;
1528 *dest = dest_func_option;
1529 }
1530 else
1531 {
1532 *type = &t_string;
1533 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001534 break;
1535 case gov_bool:
1536 case gov_hidden_bool:
1537 *type = &t_bool;
1538 break;
1539 case gov_number:
1540 case gov_hidden_number:
1541 *type = &t_number;
1542 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001543 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001544 }
1545 else if (*name == '$')
1546 {
1547 *dest = dest_env;
1548 *type = &t_string;
1549 }
1550 else if (*name == '@')
1551 {
Bram Moolenaar3558afe2022-10-13 16:12:57 +01001552 if (!valid_dest_reg(name[1]))
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001553 return FAIL;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001554 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02001555 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001556 }
1557 else if (STRNCMP(name, "g:", 2) == 0)
1558 {
1559 *dest = dest_global;
1560 }
1561 else if (STRNCMP(name, "b:", 2) == 0)
1562 {
1563 *dest = dest_buffer;
1564 }
1565 else if (STRNCMP(name, "w:", 2) == 0)
1566 {
1567 *dest = dest_window;
1568 }
1569 else if (STRNCMP(name, "t:", 2) == 0)
1570 {
1571 *dest = dest_tab;
1572 }
1573 else if (STRNCMP(name, "v:", 2) == 0)
1574 {
1575 typval_T *vtv;
1576 int di_flags;
1577
1578 *vimvaridx = find_vim_var(name + 2, &di_flags);
1579 if (*vimvaridx < 0)
1580 {
1581 semsg(_(e_variable_not_found_str), name);
1582 return FAIL;
1583 }
1584 // We use the current value of "sandbox" here, is that OK?
1585 if (var_check_ro(di_flags, name, FALSE))
1586 return FAIL;
1587 *dest = dest_vimvar;
1588 vtv = get_vim_var_tv(*vimvaridx);
1589 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
1590 }
1591 return OK;
1592}
1593
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001594 static int
Bram Moolenaar97f8c102022-04-02 19:43:57 +01001595is_decl_command(cmdidx_T cmdidx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001596{
1597 return cmdidx == CMD_let || cmdidx == CMD_var
1598 || cmdidx == CMD_final || cmdidx == CMD_const;
1599}
1600
1601/*
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02001602 * Returns TRUE if the class or object variable in "lhs" is modifiable.
1603 * "var_start" points to the start of the variable name and "lhs->lhs_varlen"
1604 * has the total length. Note that the "lhs" can be nested an object reference
1605 * (e.g. a.b.c.d.var).
1606 */
1607 static int
1608lhs_class_member_modifiable(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
1609{
1610 size_t varlen = lhs->lhs_varlen;
1611 class_T *cl = lhs->lhs_type->tt_class;
1612 int is_object = lhs->lhs_type->tt_type == VAR_OBJECT;
1613 char_u *name = var_start + varlen + 1;
1614 size_t namelen = lhs->lhs_end - var_start - varlen - 1;
1615 ocmember_T *m;
1616
1617 m = member_lookup(cl, lhs->lhs_type->tt_type, name, namelen, NULL);
1618 if (m == NULL)
1619 {
1620 member_not_found_msg(cl, lhs->lhs_type->tt_type, name, namelen);
1621 return FALSE;
1622 }
1623
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01001624 if (IS_ENUM(cl))
1625 {
1626 semsg(_(e_enumvalue_str_cannot_be_modified), cl->class_name,
1627 m->ocm_name);
1628 return FALSE;
1629 }
1630
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02001631 // If it is private member variable, then accessing it outside the
1632 // class is not allowed.
1633 // If it is a read only class variable, then it can be modified
1634 // only inside the class where it is defined.
1635 if ((m->ocm_access != VIM_ACCESS_ALL) &&
1636 ((is_object && !inside_class(cctx, cl))
1637 || (!is_object && cctx->ctx_ufunc->uf_class != cl)))
1638 {
1639 char *msg = (m->ocm_access == VIM_ACCESS_PRIVATE)
Ernie Rael03042a22023-11-11 08:53:32 +01001640 ? e_cannot_access_protected_variable_str
RestorerZ7fe8f432023-09-24 23:21:24 +02001641 : e_variable_is_not_writable_str;
Ernie Raele6c9aa52023-10-06 19:55:52 +02001642 emsg_var_cl_define(msg, m->ocm_name, 0, cl);
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02001643 return FALSE;
1644 }
1645
1646 return TRUE;
1647}
1648
1649/*
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001650 * Initialize "lhs" with default values
Bram Moolenaar752fc692021-01-04 21:57:11 +01001651 */
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001652 static void
1653lhs_init_defaults(lhs_T *lhs)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001654{
Bram Moolenaar752fc692021-01-04 21:57:11 +01001655 CLEAR_POINTER(lhs);
1656 lhs->lhs_dest = dest_local;
1657 lhs->lhs_vimvaridx = -1;
1658 lhs->lhs_scriptvar_idx = -1;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001659 lhs->lhs_member_idx = -1;
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001660}
Bram Moolenaar752fc692021-01-04 21:57:11 +01001661
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001662/*
1663 * When compiling a LHS variable name, find the end of the destination and the
1664 * end of the variable name.
1665 */
1666 static int
1667lhs_find_var_end(
1668 lhs_T *lhs,
1669 char_u *var_start,
1670 int is_decl,
1671 char_u **var_endp)
1672{
1673 char_u *var_end = *var_endp;
1674
1675 // "lhs_dest_end" is the end of the destination, including "[expr]" or
Bram Moolenaar752fc692021-01-04 21:57:11 +01001676 // ".name".
1677 // "var_end" is the end of the variable/option/etc. name.
1678 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
1679 if (*var_start == '@')
Bram Moolenaar3558afe2022-10-13 16:12:57 +01001680 {
1681 if (!valid_dest_reg(var_start[1]))
1682 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001683 var_end = var_start + 2;
Bram Moolenaar3558afe2022-10-13 16:12:57 +01001684 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01001685 else
1686 {
1687 // skip over the leading "&", "&l:", "&g:" and "$"
1688 var_end = skip_option_env_lead(var_start);
1689 var_end = to_name_end(var_end, TRUE);
1690 }
1691
1692 // "a: type" is declaring variable "a" with a type, not dict "a:".
1693 if (is_decl && lhs->lhs_dest_end == var_start + 2
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001694 && lhs->lhs_dest_end[-1] == ':')
Bram Moolenaar752fc692021-01-04 21:57:11 +01001695 --lhs->lhs_dest_end;
1696 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
1697 --var_end;
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001698
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00001699 lhs->lhs_end = lhs->lhs_dest_end;
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001700 *var_endp = var_end;
1701
1702 return OK;
1703}
1704
1705/*
1706 * Set various fields in "lhs"
1707 */
1708 static int
1709lhs_init(
1710 lhs_T *lhs,
1711 char_u *var_start,
1712 int is_decl,
1713 int heredoc,
1714 char_u **var_endp)
1715{
1716 char_u *var_end = *var_endp;
1717
1718 lhs_init_defaults(lhs);
1719
1720 // Find the end of the variable and the destination
1721 if (lhs_find_var_end(lhs, var_start, is_decl, &var_end) == FAIL)
1722 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001723
1724 // compute the length of the destination without "[expr]" or ".name"
1725 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02001726 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001727 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
1728 if (lhs->lhs_name == NULL)
1729 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01001730
1731 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
1732 // Something follows after the variable: "var[idx]" or "var.key".
1733 lhs->lhs_has_index = TRUE;
1734
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001735 lhs->lhs_type = heredoc ? &t_list_string : &t_any;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001736
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001737 *var_endp = var_end;
1738
1739 return OK;
1740}
1741
1742/*
1743 * Compile a LHS class variable name.
1744 */
1745 static int
1746compile_lhs_class_variable(
1747 cctx_T *cctx,
1748 lhs_T *lhs,
1749 class_T *defcl,
1750 int is_decl)
1751{
1752 if (cctx->ctx_ufunc->uf_defclass != defcl)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001753 {
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001754 // A class variable can be accessed without the class name
1755 // only inside a class.
1756 semsg(_(e_class_variable_str_accessible_only_inside_class_str),
1757 lhs->lhs_name, defcl->class_name);
1758 return FAIL;
1759 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01001760
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001761 if (is_decl)
1762 {
1763 semsg(_(e_variable_already_declared_in_class_str), lhs->lhs_name);
1764 return FAIL;
1765 }
1766
1767 ocmember_T *m = &defcl->class_class_members[lhs->lhs_classmember_idx];
1768 if (oc_var_check_ro(defcl, m))
1769 return FAIL;
1770
1771 lhs->lhs_dest = dest_class_member;
1772 // The class variable is defined either in the current class or
1773 // in one of the parent class in the hierarchy.
1774 lhs->lhs_class = defcl;
1775 lhs->lhs_type = oc_member_type_by_idx(defcl, FALSE,
1776 lhs->lhs_classmember_idx);
1777
1778 return OK;
1779}
1780
1781/*
1782 * Compile an imported LHS variable
1783 */
1784 static int
1785compile_lhs_import_var(
1786 lhs_T *lhs,
1787 imported_T *import,
1788 char_u *var_start,
1789 char_u **var_endp,
1790 char_u **rawnamep)
1791{
1792 char_u *var_end = *var_endp;
1793 char_u *dot = vim_strchr(var_start, '.');
1794 char_u *p;
1795
1796 // for an import the name is what comes after the dot
1797 if (dot == NULL)
1798 {
1799 semsg(_(e_no_dot_after_imported_name_str), var_start);
1800 return FAIL;
1801 }
1802
1803 p = skipwhite(dot + 1);
1804 var_end = to_name_end(p, TRUE);
1805 if (var_end == p)
1806 {
1807 semsg(_(e_missing_name_after_imported_name_str), var_start);
1808 return FAIL;
1809 }
1810
1811 vim_free(lhs->lhs_name);
1812 lhs->lhs_varlen = var_end - p;
1813 lhs->lhs_name = vim_strnsave(p, lhs->lhs_varlen);
1814 if (lhs->lhs_name == NULL)
1815 return FAIL;
1816 *rawnamep = lhs->lhs_name;
1817 lhs->lhs_scriptvar_sid = import->imp_sid;
1818
1819 // TODO: where do we check this name is exported?
1820
1821 // Check if something follows: "exp.var[idx]" or
1822 // "exp.var.key".
1823 lhs->lhs_has_index = lhs->lhs_dest_end > skipwhite(var_end);
1824
1825 *var_endp = var_end;
1826
1827 return OK;
1828}
1829
1830/*
1831 * Process a script-local variable when compiling a LHS variable name.
1832 */
1833 static int
1834compile_lhs_script_var(
1835 cctx_T *cctx,
1836 lhs_T *lhs,
1837 char_u *var_start,
1838 char_u *var_end,
1839 int is_decl)
1840{
1841 int script_namespace = FALSE;
1842 int script_var = FALSE;
1843 imported_T *import;
1844 char_u *var_name;
Yegappan Lakshmanan084529c2024-12-24 09:50:01 +01001845 size_t var_name_len;
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001846
1847 if (lhs->lhs_varlen > 1 && STRNCMP(var_start, "s:", 2) == 0)
1848 script_namespace = TRUE;
1849
1850 if (script_namespace)
1851 {
1852 var_name = var_start + 2;
1853 var_name_len = lhs->lhs_varlen - 2;
1854 }
1855 else
1856 {
1857 var_name = var_start;
1858 var_name_len = lhs->lhs_varlen;
1859 }
1860
1861 if (script_var_exists(var_name, var_name_len, cctx, NULL) == OK)
1862 script_var = TRUE;
1863
1864 import = find_imported(var_start, lhs->lhs_varlen, FALSE);
1865
1866 if (script_namespace || script_var || import != NULL)
1867 {
1868 char_u *rawname = lhs->lhs_name + (lhs->lhs_name[1] == ':' ? 2 : 0);
1869
1870 if (script_namespace && current_script_is_vim9())
Bram Moolenaar752fc692021-01-04 21:57:11 +01001871 {
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001872 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), var_start);
1873 return FAIL;
1874 }
1875
1876 if (is_decl)
1877 {
1878 if (script_namespace)
1879 semsg(_(e_cannot_declare_script_variable_in_function_str),
1880 lhs->lhs_name);
1881 else
1882 semsg(_(e_variable_already_declared_in_script_str),
1883 lhs->lhs_name);
1884 return FAIL;
1885 }
1886 else if (cctx->ctx_ufunc->uf_script_ctx_version == SCRIPT_VERSION_VIM9
1887 && script_namespace
1888 && !script_var && import == NULL)
1889 {
1890 semsg(_(e_unknown_variable_str), lhs->lhs_name);
1891 return FAIL;
1892 }
1893
1894 lhs->lhs_dest = current_script_is_vim9() ? dest_script_v9 :
1895 dest_script;
1896
1897 // existing script-local variables should have a type
1898 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
1899 if (import != NULL)
1900 {
1901 if (compile_lhs_import_var(lhs, import, var_start, &var_end,
1902 &rawname) == FAIL)
1903 return FAIL;
1904 }
1905
1906 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
1907 {
1908 // Check writable only when no index follows.
1909 lhs->lhs_scriptvar_idx = get_script_item_idx(
1910 lhs->lhs_scriptvar_sid, rawname,
1911 lhs->lhs_has_index ? ASSIGN_FINAL :
1912 ASSIGN_CONST, cctx, NULL);
1913 if (lhs->lhs_scriptvar_idx >= 0)
1914 {
1915 scriptitem_T *si = SCRIPT_ITEM(lhs->lhs_scriptvar_sid);
1916 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1917 + lhs->lhs_scriptvar_idx;
1918
1919 lhs->lhs_type = sv->sv_type;
1920 }
1921 }
1922
1923 return OK;
1924 }
1925
1926 return check_defined(var_start, lhs->lhs_varlen, cctx, NULL, FALSE);
1927}
1928
1929/*
1930 * Compile the LHS destination.
1931 */
1932 static int
1933compile_lhs_var_dest(
1934 cctx_T *cctx,
1935 lhs_T *lhs,
1936 int cmdidx,
1937 char_u *var_start,
1938 char_u *var_end,
1939 int is_decl)
1940{
1941 int declare_error = FALSE;
1942
1943 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
1944 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
1945 &lhs->lhs_type, cctx) == FAIL)
1946 return FAIL;
1947
1948 if (lhs->lhs_dest != dest_local && cmdidx != CMD_const
1949 && cmdidx != CMD_final)
1950 {
1951 // Specific kind of variable recognized.
1952 declare_error = is_decl;
1953 }
1954 else
1955 {
1956 class_T *defcl;
1957
1958 // No specific kind of variable recognized, just a name.
1959 if (check_reserved_name(lhs->lhs_name, lhs->lhs_has_index
1960 && *var_end == '.') == FAIL)
1961 return FAIL;
1962
1963 if (lookup_local(var_start, lhs->lhs_varlen, &lhs->lhs_local_lvar,
1964 cctx) == OK)
1965 {
1966 lhs->lhs_lvar = &lhs->lhs_local_lvar;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001967 }
1968 else
1969 {
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001970 CLEAR_FIELD(lhs->lhs_arg_lvar);
1971 if (arg_exists(var_start, lhs->lhs_varlen,
1972 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
1973 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001974 {
1975 if (is_decl)
1976 {
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001977 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
Bram Moolenaar752fc692021-01-04 21:57:11 +01001978 return FAIL;
1979 }
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001980 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001981 }
1982 }
1983
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001984 if (lhs->lhs_lvar != NULL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001985 {
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001986 if (is_decl)
1987 {
1988 // if we come here with what looks like an assignment like
1989 // .= but which has been rejected by assignment_len() from
1990 // may_compile_assignment give a better error message
1991 char_u *p = skipwhite(lhs->lhs_end);
1992 if (p[0] == '.' && p[1] == '=')
1993 emsg(_(e_dot_equal_not_supported_with_script_version_two));
1994 else if (p[0] == ':')
1995 // type specified in a non-var assignment
1996 semsg(_(e_trailing_characters_str), p);
1997 else
1998 semsg(_(e_variable_already_declared_str), lhs->lhs_name);
1999 return FAIL;
2000 }
2001 }
2002 else if ((lhs->lhs_classmember_idx = cctx_class_member_idx(
2003 cctx, var_start, lhs->lhs_varlen, &defcl)) >= 0)
2004 {
2005 if (compile_lhs_class_variable(cctx, lhs, defcl, is_decl)
2006 == FAIL)
2007 return FAIL;
2008 }
2009 else
2010 {
2011 if (compile_lhs_script_var(cctx, lhs, var_start, var_end,
2012 is_decl) == FAIL)
2013 return FAIL;
2014 }
2015 }
2016
2017 if (declare_error)
2018 {
2019 vim9_declare_error(lhs->lhs_name);
2020 return FAIL;
2021 }
2022
2023 return OK;
2024}
2025
2026/*
2027 * When compiling a LHS variable name, for a class or an object, set the LHS
2028 * member type.
2029 */
2030 static int
2031compile_lhs_set_oc_member_type(
2032 cctx_T *cctx,
2033 lhs_T *lhs,
2034 char_u *var_start)
2035{
2036 class_T *cl = lhs->lhs_type->tt_class;
2037 int is_object = lhs->lhs_type->tt_type == VAR_OBJECT;
2038 char_u *name = var_start + lhs->lhs_varlen + 1;
2039 size_t namelen = lhs->lhs_end - var_start - lhs->lhs_varlen - 1;
2040
2041 ocmember_T *m = member_lookup(cl, lhs->lhs_type->tt_type,
2042 name, namelen, &lhs->lhs_member_idx);
2043 if (m == NULL)
2044 {
2045 member_not_found_msg(cl, lhs->lhs_type->tt_type, name, namelen);
2046 return FAIL;
2047 }
2048
2049 if (IS_ENUM(cl))
2050 {
2051 if (!inside_class(cctx, cl))
2052 {
2053 semsg(_(e_enumvalue_str_cannot_be_modified),
2054 cl->class_name, m->ocm_name);
Bram Moolenaar752fc692021-01-04 21:57:11 +01002055 return FAIL;
2056 }
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002057 if (lhs->lhs_type->tt_type == VAR_OBJECT &&
2058 lhs->lhs_member_idx < 2)
2059 {
2060 char *msg = lhs->lhs_member_idx == 0 ?
2061 e_enum_str_name_cannot_be_modified :
2062 e_enum_str_ordinal_cannot_be_modified;
2063 semsg(_(msg), cl->class_name);
2064 return FAIL;
2065 }
2066 }
2067
2068 // If it is private member variable, then accessing it outside the
2069 // class is not allowed.
2070 // If it is a read only class variable, then it can be modified
2071 // only inside the class where it is defined.
2072 if ((m->ocm_access != VIM_ACCESS_ALL) &&
2073 ((is_object && !inside_class(cctx, cl))
2074 || (!is_object && cctx->ctx_ufunc->uf_class != cl)))
2075 {
2076 char *msg = (m->ocm_access == VIM_ACCESS_PRIVATE)
2077 ? e_cannot_access_protected_variable_str
2078 : e_variable_is_not_writable_str;
2079 emsg_var_cl_define(msg, m->ocm_name, 0, cl);
2080 return FAIL;
2081 }
2082
2083 if (!IS_CONSTRUCTOR_METHOD(cctx->ctx_ufunc)
2084 && oc_var_check_ro(cl, m))
2085 return FAIL;
2086
2087 lhs->lhs_member_type = m->ocm_type;
2088
2089 return OK;
2090}
2091
2092/*
2093 * When compiling a LHS variable, set the LHS variable type.
2094 */
2095 static int
2096compile_lhs_set_type(cctx_T *cctx, lhs_T *lhs, char_u *var_end, int is_decl)
2097{
2098 if (is_decl && *skipwhite(var_end) == ':')
2099 {
2100 char_u *p;
2101
2102 // parse optional type: "let var: type = expr"
2103 if (VIM_ISWHITE(*var_end))
2104 {
2105 semsg(_(e_no_white_space_allowed_before_colon_str), var_end);
2106 return FAIL;
2107 }
2108
2109 if (!VIM_ISWHITE(var_end[1]))
2110 {
2111 semsg(_(e_white_space_required_after_str_str), ":", var_end);
2112 return FAIL;
2113 }
2114
2115 p = skipwhite(var_end + 1);
2116 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
2117 if (lhs->lhs_type == NULL)
2118 return FAIL;
2119
2120 lhs->lhs_has_type = TRUE;
2121 lhs->lhs_end = p;
2122 }
2123 else if (lhs->lhs_lvar != NULL)
2124 lhs->lhs_type = lhs->lhs_lvar->lv_type;
2125
2126 return OK;
2127}
2128
2129/*
2130 * Returns TRUE if "lhs" is a concatenable string.
2131 */
2132 static int
2133lhs_concatenable(lhs_T *lhs)
2134{
2135 return lhs->lhs_dest == dest_global
2136 || lhs->lhs_has_index
2137 || lhs->lhs_type->tt_type == VAR_STRING
2138 || lhs->lhs_type->tt_type == VAR_ANY;
2139}
2140
2141/*
2142 * Create a new local variable when compiling a LHS variable.
2143 */
2144 static int
2145compile_lhs_new_local_var(
2146 cctx_T *cctx,
2147 lhs_T *lhs,
2148 char_u *var_start,
2149 int cmdidx,
2150 int oplen,
2151 int is_decl,
2152 int has_cmd,
2153 int heredoc)
2154{
2155 if (oplen > 1 && !heredoc)
2156 {
2157 // +=, /=, etc. require an existing variable
2158 semsg(_(e_cannot_use_operator_on_new_variable_str), lhs->lhs_name);
2159 return FAIL;
2160 }
2161
2162 if (!is_decl || (lhs->lhs_has_index && !has_cmd
2163 && cctx->ctx_skip != SKIP_YES))
2164 {
2165 semsg(_(e_unknown_variable_str), lhs->lhs_name);
2166 return FAIL;
2167 }
2168
2169 // Check the name is valid for a funcref.
2170 if (lhs->lhs_type->tt_type == VAR_FUNC
2171 || lhs->lhs_type->tt_type == VAR_PARTIAL)
2172 {
2173 if (var_wrong_func_name(lhs->lhs_name, TRUE))
2174 return FAIL;
2175 }
2176
2177 // New local variable.
2178 int assign;
2179 switch (cmdidx)
2180 {
2181 case CMD_final:
2182 assign = ASSIGN_FINAL; break;
2183 case CMD_const:
2184 assign = ASSIGN_CONST; break;
2185 default:
2186 assign = ASSIGN_VAR; break;
2187 }
2188
2189 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen, assign,
2190 lhs->lhs_type);
2191 if (lhs->lhs_lvar == NULL)
2192 return FAIL;
2193
2194 lhs->lhs_new_local = TRUE;
2195
2196 return OK;
2197}
2198
2199/*
2200 * When compiling a LHS variable name, set the LHS member type.
2201 */
2202 static int
2203compile_lhs_set_member_type(
2204 cctx_T *cctx,
2205 lhs_T *lhs,
2206 char_u *var_start,
2207 int is_decl,
2208 int has_cmd)
2209{
2210 lhs->lhs_member_type = lhs->lhs_type;
2211
2212 if (!lhs->lhs_has_index)
2213 return OK;
2214
2215 char_u *after = var_start + lhs->lhs_varlen;
2216 char_u *p;
2217
2218 // Something follows after the variable: "var[idx]" or "var.key".
2219 if (is_decl && cctx->ctx_skip != SKIP_YES)
2220 {
2221 if (has_cmd)
2222 emsg(_(e_cannot_use_index_when_declaring_variable));
2223 else
2224 semsg(_(e_unknown_variable_str), lhs->lhs_name);
2225 return FAIL;
2226 }
2227
2228 // Now: var_start[lhs->lhs_varlen] is '[' or '.'
2229 // Only the last index is used below, if there are others
2230 // before it generate code for the expression. Thus for
2231 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
2232 for (;;)
2233 {
2234 p = skip_index(after);
2235 if (*p != '[' && *p != '.')
2236 {
2237 lhs->lhs_varlen_total = p - var_start;
2238 break;
2239 }
2240 after = p;
2241 }
2242 if (after > var_start + lhs->lhs_varlen)
2243 {
2244 lhs->lhs_varlen = after - var_start;
2245 lhs->lhs_dest = dest_expr;
2246 // We don't know the type before evaluating the expression,
2247 // use "any" until then.
2248 lhs->lhs_type = &t_any;
2249 }
2250
2251 int use_class = lhs->lhs_type != NULL
2252 && (lhs->lhs_type->tt_type == VAR_CLASS
2253 || lhs->lhs_type->tt_type == VAR_OBJECT);
2254
2255 if (lhs->lhs_type == NULL
2256 || (use_class ? lhs->lhs_type->tt_class == NULL
2257 : lhs->lhs_type->tt_member == NULL))
2258 {
2259 lhs->lhs_member_type = &t_any;
2260 }
2261 else if (use_class)
2262 {
2263 // for an object or class member get the type of the member
2264 if (compile_lhs_set_oc_member_type(cctx, lhs, var_start) == FAIL)
2265 return FAIL;
2266 }
2267 else
2268 lhs->lhs_member_type = lhs->lhs_type->tt_member;
2269
2270 return OK;
2271}
2272
2273/*
2274 * Figure out the LHS type and other properties for an assignment or one item
2275 * of ":unlet" with an index.
2276 * Returns OK or FAIL.
2277 */
2278 int
2279compile_lhs(
2280 char_u *var_start,
2281 lhs_T *lhs,
2282 cmdidx_T cmdidx,
2283 int heredoc,
2284 int has_cmd, // "var" before "var_start"
2285 int oplen,
2286 cctx_T *cctx)
2287{
Yegappan Lakshmanan59c88802024-12-19 20:00:31 +01002288 char_u *var_end = NULL;
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002289 int is_decl = is_decl_command(cmdidx);
2290
2291 if (lhs_init(lhs, var_start, is_decl, heredoc, &var_end) == FAIL)
2292 return FAIL;
2293
2294 if (cctx->ctx_skip != SKIP_YES)
2295 {
2296 // compile the LHS destination
2297 if (compile_lhs_var_dest(cctx, lhs, cmdidx, var_start, var_end,
2298 is_decl) == FAIL)
2299 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002300 }
2301
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00002302 // handle "a:name" as a name, not index "name" in "a"
Bram Moolenaar752fc692021-01-04 21:57:11 +01002303 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
2304 var_end = lhs->lhs_dest_end;
2305
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002306 if (lhs->lhs_dest != dest_option && lhs->lhs_dest != dest_func_option)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002307 {
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002308 // set the LHS variable type
2309 if (compile_lhs_set_type(cctx, lhs, var_end, is_decl) == FAIL)
2310 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002311 }
2312
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002313 if (oplen == 3 && !heredoc && !lhs_concatenable(lhs))
Bram Moolenaar752fc692021-01-04 21:57:11 +01002314 {
2315 emsg(_(e_can_only_concatenate_to_string));
2316 return FAIL;
2317 }
2318
2319 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002320 && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002321 {
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002322 if (compile_lhs_new_local_var(cctx, lhs, var_start, cmdidx, oplen,
2323 is_decl, has_cmd, heredoc) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002324 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002325 }
2326
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002327 if (compile_lhs_set_member_type(cctx, lhs, var_start, is_decl, has_cmd)
2328 == FAIL)
2329 return FAIL;
Bram Moolenaarc750d912021-11-29 22:02:12 +00002330
Bram Moolenaar752fc692021-01-04 21:57:11 +01002331 return OK;
2332}
2333
2334/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002335 * Figure out the LHS and check a few errors.
2336 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002337 int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002338compile_assign_lhs(
Bram Moolenaar97f8c102022-04-02 19:43:57 +01002339 char_u *var_start,
2340 lhs_T *lhs,
2341 cmdidx_T cmdidx,
2342 int is_decl,
2343 int heredoc,
2344 int has_cmd, // "var" before "var_start"
2345 int oplen,
2346 cctx_T *cctx)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002347{
Bram Moolenaar97f8c102022-04-02 19:43:57 +01002348 if (compile_lhs(var_start, lhs, cmdidx, heredoc, has_cmd, oplen, cctx)
2349 == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002350 return FAIL;
2351
2352 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
2353 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002354 semsg(_(e_cannot_assign_to_argument_str), lhs->lhs_name);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002355 return FAIL;
2356 }
2357 if (!is_decl && lhs->lhs_lvar != NULL
Bram Moolenaar6586a012022-09-30 11:04:50 +01002358 && lhs->lhs_lvar->lv_const != ASSIGN_VAR
2359 && !lhs->lhs_has_index)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002360 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002361 semsg(_(e_cannot_assign_to_constant_str), lhs->lhs_name);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002362 return FAIL;
2363 }
2364 return OK;
2365}
2366
2367/*
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002368 * Return TRUE if "lhs" has a range index: "[expr : expr]".
2369 */
2370 static int
2371has_list_index(char_u *idx_start, cctx_T *cctx)
2372{
2373 char_u *p = idx_start;
2374 int save_skip;
2375
2376 if (*p != '[')
2377 return FALSE;
2378
2379 p = skipwhite(p + 1);
2380 if (*p == ':')
2381 return TRUE;
2382
2383 save_skip = cctx->ctx_skip;
2384 cctx->ctx_skip = SKIP_YES;
2385 (void)compile_expr0(&p, cctx);
2386 cctx->ctx_skip = save_skip;
2387 return *skipwhite(p) == ':';
2388}
2389
2390/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002391 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
2392 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01002393 */
2394 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02002395compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01002396 char_u *var_start,
2397 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002398 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01002399 cctx_T *cctx)
2400{
Bram Moolenaar752fc692021-01-04 21:57:11 +01002401 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002402 char_u *p;
2403 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02002404 int need_white_before = TRUE;
2405 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002406
Bram Moolenaar752fc692021-01-04 21:57:11 +01002407 p = var_start + varlen;
2408 if (*p == '[')
2409 {
2410 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02002411 if (*p == ':')
2412 {
2413 // empty first index, push zero
2414 r = generate_PUSHNR(cctx, 0);
2415 need_white_before = FALSE;
2416 }
2417 else
2418 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002419
2420 if (r == OK && *skipwhite(p) == ':')
2421 {
2422 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02002423 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02002424 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002425 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02002426 empty_second = *skipwhite(p + 1) == ']';
2427 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
2428 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002429 {
2430 semsg(_(e_white_space_required_before_and_after_str_at_str),
2431 ":", p);
2432 return FAIL;
2433 }
2434 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02002435 if (*p == ']')
2436 // empty second index, push "none"
2437 r = generate_PUSHSPEC(cctx, VVAL_NONE);
2438 else
2439 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002440 }
2441
Bram Moolenaar752fc692021-01-04 21:57:11 +01002442 if (r == OK && *skipwhite(p) != ']')
2443 {
2444 // this should not happen
Bram Moolenaare1242042021-12-16 20:56:57 +00002445 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar752fc692021-01-04 21:57:11 +01002446 r = FAIL;
2447 }
2448 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002449 else if (lhs->lhs_member_idx >= 0)
2450 {
2451 // object member index
2452 r = generate_PUSHNR(cctx, lhs->lhs_member_idx);
2453 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002454 else // if (*p == '.')
2455 {
2456 char_u *key_end = to_name_end(p + 1, TRUE);
2457 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
2458
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02002459 r = generate_PUSHS(cctx, &key);
Bram Moolenaar752fc692021-01-04 21:57:11 +01002460 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02002461 return r;
2462}
2463
2464/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002465 * For a LHS with an index, load the variable to be indexed.
2466 */
2467 static int
2468compile_load_lhs(
2469 lhs_T *lhs,
2470 char_u *var_start,
2471 type_T *rhs_type,
2472 cctx_T *cctx)
2473{
2474 if (lhs->lhs_dest == dest_expr)
2475 {
2476 size_t varlen = lhs->lhs_varlen;
2477 int c = var_start[varlen];
Bram Moolenaare97976b2021-08-01 13:17:17 +02002478 int lines_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaare97976b2021-08-01 13:17:17 +02002479 int res;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002480
Bram Moolenaare97976b2021-08-01 13:17:17 +02002481 // Evaluate "ll[expr]" of "ll[expr][idx]". End the line with a NUL and
2482 // limit the lines array length to avoid skipping to a following line.
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002483 var_start[varlen] = NUL;
Bram Moolenaare97976b2021-08-01 13:17:17 +02002484 cctx->ctx_ufunc->uf_lines.ga_len = cctx->ctx_lnum + 1;
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002485 char_u *p = var_start;
Bram Moolenaare97976b2021-08-01 13:17:17 +02002486 res = compile_expr0(&p, cctx);
2487 var_start[varlen] = c;
2488 cctx->ctx_ufunc->uf_lines.ga_len = lines_len;
2489 if (res == FAIL || p != var_start + varlen)
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002490 {
2491 // this should not happen
Bram Moolenaare97976b2021-08-01 13:17:17 +02002492 if (res != FAIL)
Bram Moolenaare1242042021-12-16 20:56:57 +00002493 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002494 return FAIL;
2495 }
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002496
Bram Moolenaar078a4612022-01-04 15:17:03 +00002497 lhs->lhs_type = cctx->ctx_type_stack.ga_len == 0 ? &t_void
2498 : get_type_on_stack(cctx, 0);
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02002499
Yegappan Lakshmanan54d7f182025-02-10 21:35:07 +01002500 if (lhs->lhs_type->tt_type == VAR_CLASS
Yegappan Lakshmanande8f8f72025-04-01 20:43:36 +02002501 || (lhs->lhs_type->tt_type == VAR_OBJECT
2502 && lhs->lhs_type != &t_object_any))
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02002503 {
Yegappan Lakshmanan54d7f182025-02-10 21:35:07 +01002504 // Check whether the class or object variable is modifiable
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02002505 if (!lhs_class_member_modifiable(lhs, var_start, cctx))
2506 return FAIL;
2507 }
2508
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002509 // Now we can properly check the type. The variable is indexed, thus
2510 // we need the member type. For a class or object we don't know the
2511 // type yet, it depends on what member is used.
Yegappan Lakshmananc229a6a2023-10-26 23:05:07 +02002512 // The top item in the stack is the Dict, followed by the key and then
2513 // the type of the value.
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002514 vartype_T vartype = lhs->lhs_type->tt_type;
2515 type_T *member_type = lhs->lhs_type->tt_member;
2516 if (rhs_type != NULL && member_type != NULL
2517 && vartype != VAR_OBJECT && vartype != VAR_CLASS
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002518 && rhs_type != &t_void
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002519 && need_type(rhs_type, member_type, FALSE,
Yegappan Lakshmananc229a6a2023-10-26 23:05:07 +02002520 -3, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002521 return FAIL;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002522
2523 return OK;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002524 }
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002525
Yegappan Lakshmanande8f8f72025-04-01 20:43:36 +02002526 return generate_loadvar(cctx, lhs);
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002527}
2528
2529/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02002530 * Produce code for loading "lhs" and also take care of an index.
2531 * Return OK/FAIL.
2532 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002533 int
Bram Moolenaara369c3d2021-04-21 16:00:10 +02002534compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
2535{
Bram Moolenaar4cae8452023-01-15 15:51:48 +00002536 if (lhs->lhs_type->tt_type == VAR_OBJECT)
2537 {
Bram Moolenaar22363c62023-04-24 17:15:25 +01002538 // "this.value": load "this" object and get the value at index for an
2539 // object or class member get the type of the member.
2540 // Also for "obj.value".
Yegappan Lakshmananff6f0d52023-12-21 16:46:18 +01002541 char_u *dot = vim_strchr(var_start, '.');
2542 if (dot == NULL)
2543 {
2544 semsg(_(e_missing_dot_after_object_str), lhs->lhs_name);
2545 return FAIL;
2546 }
Bram Moolenaar22363c62023-04-24 17:15:25 +01002547
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +02002548 class_T *cl = lhs->lhs_type->tt_class;
Yegappan Lakshmanan1ea42882023-10-11 21:43:52 +02002549 type_T *type = oc_member_type(cl, TRUE, dot + 1,
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02002550 lhs->lhs_end, &lhs->lhs_member_idx);
Bram Moolenaar4cae8452023-01-15 15:51:48 +00002551 if (lhs->lhs_member_idx < 0)
2552 return FAIL;
2553
Bram Moolenaar22363c62023-04-24 17:15:25 +01002554 if (dot - var_start == 4 && STRNCMP(var_start, "this", 4) == 0)
2555 {
2556 // load "this"
Yegappan Lakshmanand990bf02024-03-22 19:56:17 +01002557 lvar_T *lvar = lhs->lhs_lvar;
2558 int rc;
2559
2560 if (lvar->lv_from_outer > 0)
2561 rc = generate_LOADOUTER(cctx, lvar->lv_idx,
2562 lvar->lv_from_outer, lvar->lv_loop_depth,
2563 lvar->lv_loop_idx, type);
2564 else
2565 rc = generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
2566
2567 if (rc == FAIL)
Bram Moolenaar22363c62023-04-24 17:15:25 +01002568 return FAIL;
2569 }
2570 else
2571 {
2572 // load object variable or argument
2573 if (compile_load_lhs(lhs, var_start, lhs->lhs_type, cctx) == FAIL)
2574 return FAIL;
2575 }
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01002576 if (IS_INTERFACE(cl))
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02002577 return generate_GET_ITF_MEMBER(cctx, cl, lhs->lhs_member_idx, type);
2578 return generate_GET_OBJ_MEMBER(cctx, lhs->lhs_member_idx, type);
Bram Moolenaar4cae8452023-01-15 15:51:48 +00002579 }
Yegappan Lakshmanan1ea42882023-10-11 21:43:52 +02002580 else if (lhs->lhs_type->tt_type == VAR_CLASS)
2581 {
2582 // "<classname>.value": load class variable "classname.value"
Yegappan Lakshmananff6f0d52023-12-21 16:46:18 +01002583 char_u *dot = vim_strchr(var_start, '.');
2584 if (dot == NULL)
2585 {
2586 check_type_is_value(lhs->lhs_type);
2587 return FAIL;
2588 }
Yegappan Lakshmanan1ea42882023-10-11 21:43:52 +02002589
2590 class_T *cl = lhs->lhs_type->tt_class;
2591 ocmember_T *m = class_member_lookup(cl, dot + 1,
2592 lhs->lhs_end - dot - 1,
2593 &lhs->lhs_member_idx);
2594 if (m == NULL)
2595 return FAIL;
2596
2597 return generate_CLASSMEMBER(cctx, TRUE, cl, lhs->lhs_member_idx);
2598 }
Bram Moolenaar4cae8452023-01-15 15:51:48 +00002599
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002600 if (compile_load_lhs(lhs, var_start, NULL, cctx) == FAIL)
2601 return FAIL;
Bram Moolenaara369c3d2021-04-21 16:00:10 +02002602
2603 if (lhs->lhs_has_index)
2604 {
2605 int range = FALSE;
2606
2607 // Get member from list or dict. First compile the
2608 // index value.
2609 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
2610 return FAIL;
2611 if (range)
2612 {
2613 semsg(_(e_cannot_use_range_with_assignment_operator_str),
2614 var_start);
2615 return FAIL;
2616 }
2617
2618 // Get the member.
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002619 if (compile_member(FALSE, NULL, cctx) == FAIL)
Bram Moolenaara369c3d2021-04-21 16:00:10 +02002620 return FAIL;
2621 }
2622 return OK;
2623}
2624
2625/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002626 * Assignment to a list or dict member, or ":unlet" for the item, using the
2627 * information in "lhs".
2628 * Returns OK or FAIL.
2629 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002630 int
Bram Moolenaare42939a2021-04-05 17:11:17 +02002631compile_assign_unlet(
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002632 char_u *var_start,
2633 lhs_T *lhs,
2634 int is_assign,
2635 type_T *rhs_type,
2636 cctx_T *cctx)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002637{
Bram Moolenaare42939a2021-04-05 17:11:17 +02002638 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002639 int range = FALSE;
2640
Bram Moolenaar68452172021-04-12 21:21:02 +02002641 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002642 return FAIL;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002643 if (is_assign && range
2644 && lhs->lhs_type->tt_type != VAR_LIST
2645 && lhs->lhs_type != &t_blob
2646 && lhs->lhs_type != &t_any)
Bram Moolenaar68452172021-04-12 21:21:02 +02002647 {
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002648 if (lhs->lhs_type->tt_type == VAR_TUPLE)
2649 emsg(_(e_cannot_slice_tuple));
2650 else
2651 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
Bram Moolenaar68452172021-04-12 21:21:02 +02002652 return FAIL;
2653 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002654
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002655 if (lhs->lhs_type == NULL || lhs->lhs_type == &t_any)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002656 {
2657 // Index on variable of unknown type: check at runtime.
2658 dest_type = VAR_ANY;
2659 }
2660 else
2661 {
2662 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002663 if (dest_type == VAR_DICT && range)
2664 {
zeertzjq276410e2023-05-07 21:59:33 +01002665 emsg(_(e_cannot_use_range_with_dictionary));
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002666 return FAIL;
2667 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002668 if (dest_type == VAR_DICT
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02002669 && may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002670 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02002671 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002672 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002673 type_T *type;
2674
2675 if (range)
2676 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00002677 type = get_type_on_stack(cctx, 1);
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002678 if (need_type(type, &t_number, FALSE,
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002679 -2, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002680 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02002681 }
Bram Moolenaar078a4612022-01-04 15:17:03 +00002682 type = get_type_on_stack(cctx, 0);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00002683 if ((dest_type != VAR_BLOB && type->tt_type != VAR_SPECIAL)
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002684 && need_type(type, &t_number, FALSE,
Bram Moolenaar51e93322021-04-17 20:44:56 +02002685 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002686 return FAIL;
2687 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002688 }
2689
Bram Moolenaar4875d6a2022-08-17 15:55:51 +01002690 if (cctx->ctx_skip == SKIP_YES)
2691 return OK;
2692
Bram Moolenaar22363c62023-04-24 17:15:25 +01002693 // Load the dict, list or object. On the stack we then have:
Bram Moolenaar752fc692021-01-04 21:57:11 +01002694 // - value (for assignment, not for :unlet)
2695 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002696 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01002697 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002698 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
2699 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002700
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002701 if (dest_type == VAR_LIST
2702 || dest_type == VAR_DICT
2703 || dest_type == VAR_BLOB
2704 || dest_type == VAR_CLASS
2705 || dest_type == VAR_OBJECT
2706 || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002707 {
2708 if (is_assign)
2709 {
Bram Moolenaar68452172021-04-12 21:21:02 +02002710 if (range)
2711 {
2712 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
2713 return FAIL;
2714 }
2715 else
2716 {
2717 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01002718
Bram Moolenaar68452172021-04-12 21:21:02 +02002719 if (isn == NULL)
2720 return FAIL;
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002721 isn->isn_arg.storeindex.si_vartype = dest_type;
2722 isn->isn_arg.storeindex.si_class = NULL;
2723
2724 if (dest_type == VAR_OBJECT)
2725 {
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +00002726 class_T *cl = lhs->lhs_type->tt_class;
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002727
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01002728 if (IS_INTERFACE(cl))
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002729 {
2730 // "this.value": load "this" object and get the value
2731 // at index for an object or class member get the type
2732 // of the member
2733 isn->isn_arg.storeindex.si_class = cl;
2734 ++cl->class_refcount;
2735 }
2736 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002737 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002738 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002739 else if (range)
2740 {
2741 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
2742 return FAIL;
2743 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002744 else
2745 {
2746 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
2747 return FAIL;
2748 }
2749 }
2750 else
2751 {
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002752 if (dest_type == VAR_TUPLE)
2753 emsg(_(e_tuple_is_immutable));
2754 else
2755 emsg(_(e_indexable_type_required));
Bram Moolenaar752fc692021-01-04 21:57:11 +01002756 return FAIL;
2757 }
2758
2759 return OK;
2760}
2761
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002762/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002763 * Generate an instruction to push the default value for "vartype".
2764 * if "dest_local" is TRUE then for some types no instruction is generated.
2765 * "skip_store" is set to TRUE if no PUSH instruction is generated.
2766 * Returns OK or FAIL.
2767 */
2768 static int
2769push_default_value(
2770 cctx_T *cctx,
2771 vartype_T vartype,
2772 int dest_is_local,
2773 int *skip_store)
2774{
2775 int r = OK;
2776
2777 switch (vartype)
2778 {
2779 case VAR_BOOL:
2780 r = generate_PUSHBOOL(cctx, VVAL_FALSE);
2781 break;
2782 case VAR_FLOAT:
2783 r = generate_PUSHF(cctx, 0.0);
2784 break;
2785 case VAR_STRING:
2786 r = generate_PUSHS(cctx, NULL);
2787 break;
2788 case VAR_BLOB:
2789 r = generate_PUSHBLOB(cctx, blob_alloc());
2790 break;
2791 case VAR_FUNC:
2792 r = generate_PUSHFUNC(cctx, NULL, &t_func_void, TRUE);
2793 break;
2794 case VAR_LIST:
2795 r = generate_NEWLIST(cctx, 0, FALSE);
2796 break;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002797 case VAR_TUPLE:
2798 r = generate_NEWTUPLE(cctx, 0, FALSE);
2799 break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002800 case VAR_DICT:
2801 r = generate_NEWDICT(cctx, 0, FALSE);
2802 break;
2803 case VAR_JOB:
2804 r = generate_PUSHJOB(cctx);
2805 break;
2806 case VAR_CHANNEL:
2807 r = generate_PUSHCHANNEL(cctx);
2808 break;
Ernie Rael5c018be2023-08-27 18:40:26 +02002809 case VAR_OBJECT:
2810 r = generate_PUSHOBJ(cctx);
2811 break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002812 case VAR_NUMBER:
2813 case VAR_UNKNOWN:
2814 case VAR_ANY:
2815 case VAR_PARTIAL:
2816 case VAR_VOID:
2817 case VAR_INSTR:
2818 case VAR_CLASS:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002819 case VAR_TYPEALIAS:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002820 case VAR_SPECIAL: // cannot happen
2821 // This is skipped for local variables, they are always
2822 // initialized to zero. But in a "for" or "while" loop
2823 // the value may have been changed.
2824 if (dest_is_local && !inside_loop_scope(cctx))
2825 *skip_store = TRUE;
2826 else
2827 r = generate_PUSHNR(cctx, 0);
2828 break;
2829 }
2830 return r;
2831}
2832
2833/*
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002834 * Compile assignment context. Used when compiling an assignment statement.
2835 */
2836typedef struct cac_S cac_T;
2837struct cac_S
2838{
2839 cmdidx_T cac_cmdidx; // assignment command
2840 char_u *cac_nextc; // next character to parse
2841 lhs_T cac_lhs; // lhs of the assignment
2842 type_T *cac_rhs_type; // rhs type of an assignment
2843 char_u *cac_op; // assignment operator
2844 int cac_oplen; // assignment operator length
2845 char_u *cac_var_start; // start of the variable names
2846 char_u *cac_var_end; // end of the variable names
2847 int cac_var_count; // number of variables in assignment
2848 int cac_var_idx; // variable index in a list
2849 int cac_semicolon; // semicolon in [var1, var2; var3]
2850 garray_T *cac_instr;
2851 int cac_instr_count;
2852 int cac_incdec;
2853 int cac_did_generate_slice;
2854 int cac_is_decl;
2855 int cac_is_const;
2856 int cac_start_lnum;
2857 type_T *cac_inferred_type;
2858 int cac_skip_store;
2859};
2860
2861/*
2862 * Initialize the compile assignment context.
2863 */
2864 static void
2865compile_assign_context_init(cac_T *cac, cctx_T *cctx, int cmdidx, char_u *arg)
2866{
2867 CLEAR_FIELD(*cac);
2868 cac->cac_cmdidx = cmdidx;
2869 cac->cac_instr = &cctx->ctx_instr;
2870 cac->cac_rhs_type = &t_any;
2871 cac->cac_is_decl = is_decl_command(cmdidx);
2872 cac->cac_start_lnum = SOURCING_LNUM;
2873 cac->cac_instr_count = -1;
2874 cac->cac_var_end = arg;
2875}
2876
2877/*
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01002878 * Compile an object member variable assignment in the arguments passed to a
2879 * class new() method.
2880 *
2881 * Instruction format:
2882 *
2883 * ifargisset <n> this.<varname> = <value>
2884 *
2885 * where <n> is the index of the default argument.
2886 *
2887 * Generates the ISN_JUMP_IF_ARG_NOT_SET instruction to skip the assignment if
2888 * the value is passed as an argument to the new() method call.
2889 *
2890 * Returns OK on success.
2891 */
2892 static int
2893compile_assign_obj_new_arg(char_u **argp, cctx_T *cctx)
2894{
2895 char_u *arg = *argp;
2896
2897 arg += 11; // skip "ifargisset"
2898 int def_arg_idx = getdigits(&arg);
2899 arg = skipwhite(arg);
2900
2901 // Use a JUMP_IF_ARG_NOT_SET instruction to skip if the value was not
2902 // given and the default value is "v:none".
2903 int stack_offset = STACK_FRAME_SIZE +
2904 (cctx->ctx_ufunc->uf_va_name != NULL ? 1 : 0);
2905 int def_arg_count = cctx->ctx_ufunc->uf_def_args.ga_len;
2906 int arg_offset = def_arg_idx - def_arg_count - stack_offset;
2907
2908 if (generate_JUMP_IF_ARG(cctx, ISN_JUMP_IF_ARG_NOT_SET,
2909 arg_offset) == FAIL)
2910 return FAIL;
2911
2912 *argp = arg;
2913 return OK;
2914}
2915
2916/*
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002917 * Translate the increment (++) and decrement (--) operators to the
2918 * corresponding compound operators (+= or -=).
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01002919 *
2920 * Returns OK on success and FAIL on syntax error.
2921 */
2922 static int
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002923translate_incdec_op(exarg_T *eap, cac_T *cac)
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01002924{
2925 if (VIM_ISWHITE(eap->cmd[2]))
2926 {
2927 semsg(_(e_no_white_space_allowed_after_str_str),
2928 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
2929 return FAIL;
2930 }
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002931 cac->cac_op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
2932 cac->cac_oplen = 2;
2933 cac->cac_incdec = TRUE;
2934
2935 return OK;
2936}
2937
2938/*
2939 * Process the operator in an assignment statement.
2940 */
2941 static int
2942compile_assign_process_operator(
2943 exarg_T *eap,
2944 char_u *arg,
2945 cac_T *cac,
2946 int *heredoc,
2947 char_u **retstr)
2948{
2949 *retstr = NULL;
2950
2951 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002952 // Change an unary operator to a compound operator
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01002953 return translate_incdec_op(eap, cac);
2954
2955 char_u *sp = cac->cac_nextc;
2956 cac->cac_nextc = skipwhite(cac->cac_nextc);
2957 cac->cac_op = cac->cac_nextc;
2958 cac->cac_oplen = assignment_len(cac->cac_nextc, heredoc);
2959
2960 if (cac->cac_var_count > 0 && cac->cac_oplen == 0)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002961 {
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01002962 // can be something like "[1, 2]->func()"
2963 *retstr = arg;
2964 return FAIL;
2965 }
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002966
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01002967 // need white space before and after the operator
2968 if (cac->cac_oplen > 0 && (!VIM_ISWHITE(*sp)
2969 || !IS_WHITE_OR_NUL(cac->cac_op[cac->cac_oplen])))
2970 {
2971 error_white_both(cac->cac_op, cac->cac_oplen);
2972 return FAIL;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002973 }
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01002974
2975 return OK;
2976}
2977
2978/*
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01002979 * Find the start of an assignment statement.
2980 */
2981 static char_u *
2982compile_assign_compute_start(char_u *arg, int var_count)
2983{
2984 if (var_count > 0)
2985 // [var1, var2] = [val1, val2]
2986 // skip over the "["
2987 return skipwhite(arg + 1);
2988
2989 return arg;
2990}
2991
2992/*
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01002993 * Parse a heredoc assignment starting at "p". Returns a pointer to the
2994 * beginning of the heredoc content.
2995 */
2996 static char_u *
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002997parse_heredoc_assignment(exarg_T *eap, cctx_T *cctx, cac_T *cac)
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01002998{
2999 // [let] varname =<< [trim] {end}
3000 eap->ea_getline = exarg_getline;
3001 eap->cookie = cctx;
3002
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003003 list_T *l = heredoc_get(eap, cac->cac_nextc + 3, FALSE, TRUE);
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003004 if (l == NULL)
3005 return NULL;
3006
3007 list_free(l);
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003008 cac->cac_nextc += STRLEN(cac->cac_nextc);
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003009
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003010 return cac->cac_nextc;
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003011}
3012
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003013/*
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003014 * Check the type of a RHS expression in a list assignment statement.
3015 * The RHS expression is already compiled. So the type is on the stack.
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003016 */
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003017 static int
3018compile_assign_list_check_rhs_type(cctx_T *cctx, cac_T *cac)
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003019{
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003020 type_T *stacktype;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003021
3022 stacktype = cctx->ctx_type_stack.ga_len == 0 ? &t_void
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003023 : get_type_on_stack(cctx, 0);
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003024 if (stacktype->tt_type == VAR_VOID)
3025 {
3026 emsg(_(e_cannot_use_void_value));
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003027 return FAIL;
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003028 }
3029
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01003030 if (stacktype->tt_type != VAR_LIST && stacktype->tt_type != VAR_TUPLE
3031 && stacktype->tt_type != VAR_ANY)
3032 {
3033 emsg(_(e_list_or_tuple_required));
3034 return FAIL;
3035 }
3036
3037 if (need_type(stacktype,
3038 stacktype->tt_type == VAR_TUPLE ? &t_tuple_any : &t_list_any,
3039 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003040 return FAIL;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003041
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01003042 if (stacktype->tt_type == VAR_TUPLE)
3043 {
3044 if (stacktype->tt_argcount != 1)
3045 cac->cac_rhs_type = &t_any;
3046 else
3047 {
3048 if (stacktype->tt_flags & TTFLAG_VARARGS)
3049 cac->cac_rhs_type = stacktype->tt_args[0]->tt_member;
3050 else
3051 cac->cac_rhs_type = stacktype->tt_args[0];
3052 }
3053 }
3054 else if (stacktype->tt_member != NULL)
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003055 cac->cac_rhs_type = stacktype->tt_member;
3056
3057 return OK;
3058}
3059
3060/*
3061 * In a list assignment statement, if a constant list was used, check the
3062 * length. Returns OK if the length check succeeds. Returns FAIL otherwise.
3063 */
3064 static int
3065compile_assign_list_check_length(cctx_T *cctx, cac_T *cac)
3066{
3067 int needed_list_len;
3068 int did_check = FALSE;
3069
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003070 needed_list_len = cac->cac_semicolon
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003071 ? cac->cac_var_count - 1
3072 : cac->cac_var_count;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003073 if (cac->cac_instr->ga_len > 0)
3074 {
3075 isn_T *isn = ((isn_T *)cac->cac_instr->ga_data) +
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003076 cac->cac_instr->ga_len - 1;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003077
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01003078 if (isn->isn_type == ISN_NEWLIST || isn->isn_type == ISN_NEWTUPLE)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003079 {
3080 did_check = TRUE;
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003081 if (cac->cac_semicolon ?
3082 isn->isn_arg.number < needed_list_len
3083 : isn->isn_arg.number != needed_list_len)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003084 {
3085 semsg(_(e_expected_nr_items_but_got_nr),
3086 needed_list_len, (int)isn->isn_arg.number);
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003087 return FAIL;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003088 }
3089 }
3090 }
3091
3092 if (!did_check)
3093 generate_CHECKLEN(cctx, needed_list_len, cac->cac_semicolon);
3094
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003095 return OK;
3096}
3097
3098/*
3099 * Evaluate the expression for "[var, var] = expr" assignment.
3100 * A line break may follow the assignment operator "=".
3101 */
3102 static char_u *
3103compile_assign_list_expr(cctx_T *cctx, cac_T *cac)
3104{
3105 char_u *whitep;
3106
3107 whitep = cac->cac_op + cac->cac_oplen;
3108
3109 if (may_get_next_line_error(whitep, &cac->cac_nextc, cctx) == FAIL)
3110 return NULL;
3111
3112 // compile RHS expression
3113 if (compile_expr0(&cac->cac_nextc, cctx) == FAIL)
3114 return NULL;
3115
3116 if (cctx->ctx_skip == SKIP_YES)
3117 // no need to parse more when skipping
3118 return cac->cac_nextc;
3119
3120 if (compile_assign_list_check_rhs_type(cctx, cac) == FAIL)
3121 return NULL;
3122
3123 // If a constant list was used we can check the length right here.
3124 if (compile_assign_list_check_length(cctx, cac) == FAIL)
3125 return FAIL;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003126
3127 return cac->cac_nextc;
3128}
3129
3130/*
3131 * Find and return the end of a heredoc or a list of variables assignment
3132 * statement. For a single variable assignment statement, returns the current
3133 * end.
3134 * Returns NULL on failure.
3135 */
3136 static char_u *
3137compile_assign_compute_end(
3138 exarg_T *eap,
3139 cctx_T *cctx,
3140 cac_T *cac,
3141 int heredoc)
3142{
3143 if (heredoc)
3144 {
3145 cac->cac_nextc = parse_heredoc_assignment(eap, cctx, cac);
3146 return cac->cac_nextc;
3147 }
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003148
3149 if (cac->cac_var_count > 0)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003150 {
3151 // for "[var, var] = expr" evaluate the expression. The list of
3152 // variables are processed later.
3153 // A line break may follow the "=".
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003154 cac->cac_nextc = compile_assign_list_expr(cctx, cac);
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003155 return cac->cac_nextc;
3156 }
3157
3158 return cac->cac_var_end;
3159}
3160
3161/*
3162 * For "var = expr" evaluate the expression.
3163 */
3164 static int
3165compile_assign_single_eval_expr(cctx_T *cctx, cac_T *cac)
3166{
3167 int ret = OK;
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003168 char_u *whitep;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003169 lhs_T *lhs = &cac->cac_lhs;
3170
3171 // Compile the expression.
3172 if (cac->cac_incdec)
3173 return generate_PUSHNR(cctx, 1);
3174
3175 // Temporarily hide the new local variable here, it is
3176 // not available to this expression.
3177 if (lhs->lhs_new_local)
3178 --cctx->ctx_locals.ga_len;
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003179 whitep = cac->cac_op + cac->cac_oplen;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003180
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003181 if (may_get_next_line_error(whitep, &cac->cac_nextc, cctx) == FAIL)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003182 {
3183 if (lhs->lhs_new_local)
3184 ++cctx->ctx_locals.ga_len;
3185 return FAIL;
3186 }
3187
3188 ret = compile_expr0_ext(&cac->cac_nextc, cctx, &cac->cac_is_const);
3189 if (lhs->lhs_new_local)
3190 ++cctx->ctx_locals.ga_len;
3191
3192 return ret;
3193}
3194
3195/*
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003196 * When compiling an assignment, set the LHS type to the RHS type.
3197 */
3198 static int
3199compile_assign_set_lhs_type_from_rhs(
3200 cctx_T *cctx,
3201 cac_T *cac,
3202 lhs_T *lhs,
3203 type_T *rhs_type)
3204{
3205 if (rhs_type->tt_type == VAR_VOID)
3206 {
3207 emsg(_(e_cannot_use_void_value));
3208 return FAIL;
3209 }
3210
3211 type_T *type;
3212
3213 // An empty list or dict has a &t_unknown member, for a variable that
3214 // implies &t_any.
3215 if (rhs_type == &t_list_empty)
3216 type = &t_list_any;
3217 else if (rhs_type == &t_dict_empty)
3218 type = &t_dict_any;
3219 else if (rhs_type == &t_unknown)
3220 type = &t_any;
3221 else
3222 {
3223 type = rhs_type;
3224 cac->cac_inferred_type = rhs_type;
3225 }
3226
3227 set_var_type(lhs->lhs_lvar, type, cctx);
3228
3229 return OK;
3230}
3231
3232/*
3233 * Returns TRUE if the "rhs_type" can be assigned to the "lhs" variable.
3234 * Used when compiling an assignment statement.
3235 */
3236 static int
3237compile_assign_valid_rhs_type(
3238 cctx_T *cctx,
3239 cac_T *cac,
3240 lhs_T *lhs,
3241 type_T *rhs_type)
3242{
3243 type_T *use_type = lhs->lhs_lvar->lv_type;
3244 where_T where = WHERE_INIT;
3245
3246 // Without operator check type here, otherwise below.
3247 // Use the line number of the assignment.
3248 SOURCING_LNUM = cac->cac_start_lnum;
3249 if (cac->cac_var_count > 0)
3250 {
3251 where.wt_index = cac->cac_var_idx + 1;
3252 where.wt_kind = WT_VARIABLE;
3253 }
3254
3255 // If assigning to a list or dict member, use the member type.
3256 // Not for "list[:] =".
3257 if (lhs->lhs_has_index &&
3258 !has_list_index(cac->cac_var_start + lhs->lhs_varlen, cctx))
3259 use_type = lhs->lhs_member_type;
3260
3261 if (need_type_where(rhs_type, use_type, FALSE, -1, where, cctx, FALSE,
3262 cac->cac_is_const) == FAIL)
3263 return FALSE;
3264
3265 return TRUE;
3266}
3267
3268/*
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003269 * Compare the LHS type with the RHS type in an assignment.
3270 */
3271 static int
3272compile_assign_check_type(cctx_T *cctx, cac_T *cac)
3273{
3274 lhs_T *lhs = &cac->cac_lhs;
3275 type_T *rhs_type;
3276
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003277 rhs_type = cctx->ctx_type_stack.ga_len == 0
3278 ? &t_void
3279 : get_type_on_stack(cctx, 0);
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003280 cac->cac_rhs_type = rhs_type;
3281
3282 if (check_type_is_value(rhs_type) == FAIL)
3283 return FAIL;
3284
3285 if (lhs->lhs_lvar != NULL && (cac->cac_is_decl || !lhs->lhs_has_type))
3286 {
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003287 if (rhs_type->tt_type == VAR_FUNC
3288 || rhs_type->tt_type == VAR_PARTIAL)
3289 {
3290 // Make sure the variable name can be used as a funcref
3291 if (!lhs->lhs_has_index
3292 && var_wrong_func_name(lhs->lhs_name, TRUE))
3293 return FAIL;
3294 }
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003295
3296 if (lhs->lhs_new_local && !lhs->lhs_has_type)
3297 {
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003298 // The LHS variable doesn't have a type. Set it to the RHS type.
3299 if (compile_assign_set_lhs_type_from_rhs(cctx, cac, lhs, rhs_type)
3300 == FAIL)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003301 return FAIL;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003302 }
3303 else if (*cac->cac_op == '=')
3304 {
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003305 if (!compile_assign_valid_rhs_type(cctx, cac, lhs, rhs_type))
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003306 return FAIL;
3307 }
3308 }
3309 else
3310 {
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003311 // Assigning to a register using @r = "abc"
3312
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003313 type_T *lhs_type = lhs->lhs_member_type;
3314
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003315 // Special case: assigning to @# can use a number or a string.
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003316 // Also: can assign a number to a float.
3317 if ((lhs_type == &t_number_or_string || lhs_type == &t_float)
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003318 && rhs_type->tt_type == VAR_NUMBER)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003319 lhs_type = &t_number;
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003320
3321 if (*cac->cac_nextc != '=')
3322 {
3323 if (need_type(rhs_type, lhs_type, FALSE, -1, 0, cctx, FALSE,
3324 FALSE) == FAIL)
3325 return FAIL;
3326 }
3327 }
3328
3329 return OK;
3330}
3331
3332/*
3333 * Compile the RHS expression in an assignment statement and generate the
3334 * instructions.
3335 */
3336 static int
3337compile_assign_rhs_expr(cctx_T *cctx, cac_T *cac)
3338{
3339 cac->cac_is_const = FALSE;
3340
3341 // for "+=", "*=", "..=" etc. first load the current value
3342 if (*cac->cac_op != '='
3343 && compile_load_lhs_with_index(&cac->cac_lhs, cac->cac_var_start,
3344 cctx) == FAIL)
3345 return FAIL;
3346
3347 // For "var = expr" evaluate the expression.
3348 if (cac->cac_var_count == 0)
3349 {
3350 int ret;
3351
3352 // Compile the expression.
3353 cac->cac_instr_count = cac->cac_instr->ga_len;
3354 ret = compile_assign_single_eval_expr(cctx, cac);
3355 if (ret == FAIL)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003356 return FAIL;
3357 }
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003358 else if (cac->cac_semicolon && cac->cac_var_idx == cac->cac_var_count - 1)
3359 {
3360 // For "[var; var] = expr" get the rest of the list
3361 cac->cac_did_generate_slice = TRUE;
3362 if (generate_SLICE(cctx, cac->cac_var_count - 1) == FAIL)
3363 return FAIL;
3364 }
3365 else
3366 {
3367 // For "[var, var] = expr" get the "var_idx" item from the
3368 // list.
3369 int with_op = *cac->cac_op != '=';
3370 if (generate_GETITEM(cctx, cac->cac_var_idx, with_op) == FAIL)
3371 return FAIL;
3372 }
3373
3374 if (compile_assign_check_type(cctx, cac) == FAIL)
3375 return FAIL;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003376
3377 return OK;
3378}
3379
3380/*
3381 * Compile the RHS expression in an assignment
3382 */
3383 static int
3384compile_assign_rhs(cctx_T *cctx, cac_T *cac)
3385{
3386 lhs_T *lhs = &cac->cac_lhs;
3387
3388 if (cctx->ctx_skip == SKIP_YES)
3389 {
3390 if (cac->cac_oplen > 0 && cac->cac_var_count == 0)
3391 {
3392 // skip over the "=" and the expression
3393 cac->cac_nextc = skipwhite(cac->cac_op + cac->cac_oplen);
3394 (void)compile_expr0(&cac->cac_nextc, cctx);
3395 }
3396 return OK;
3397 }
3398
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003399 // If RHS is specified, then generate instructions for RHS expression
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003400 if (cac->cac_oplen > 0)
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003401 return compile_assign_rhs_expr(cctx, cac);
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003402
3403 if (cac->cac_cmdidx == CMD_final)
3404 {
3405 emsg(_(e_final_requires_a_value));
3406 return FAIL;
3407 }
3408
3409 if (cac->cac_cmdidx == CMD_const)
3410 {
3411 emsg(_(e_const_requires_a_value));
3412 return FAIL;
3413 }
3414
3415 if (!lhs->lhs_has_type || lhs->lhs_dest == dest_option
3416 || lhs->lhs_dest == dest_func_option)
3417 {
3418 emsg(_(e_type_or_initialization_required));
3419 return FAIL;
3420 }
3421
3422 // variables are always initialized
3423 if (GA_GROW_FAILS(cac->cac_instr, 1))
3424 return FAIL;
3425
3426 cac->cac_instr_count = cac->cac_instr->ga_len;
3427
3428 return push_default_value(cctx, lhs->lhs_member_type->tt_type,
3429 lhs->lhs_dest == dest_local,
3430 &cac->cac_skip_store);
3431}
3432
3433/*
3434 * Compile a compound op assignment statement (+=, -=, *=, %=, etc.)
3435 */
3436 static int
3437compile_assign_compound_op(cctx_T *cctx, cac_T *cac)
3438{
3439 lhs_T *lhs = &cac->cac_lhs;
3440 type_T *expected;
3441 type_T *stacktype = NULL;
3442
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01003443 if (cac->cac_lhs.lhs_type->tt_type == VAR_TUPLE)
3444 {
3445 // compound operators are not supported with a tuple
3446 char_u op[2];
3447
3448 op[0] = *cac->cac_op;
3449 op[1] = NUL;
3450 semsg(_(e_wrong_variable_type_for_str_equal), op);
3451 return FAIL;
3452 }
3453
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003454 if (*cac->cac_op == '.')
3455 {
3456 if (may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL)
3457 return FAIL;
3458 }
3459 else
3460 {
3461 expected = lhs->lhs_member_type;
3462 stacktype = get_type_on_stack(cctx, 0);
3463 if (
3464 // If variable is float operation with number is OK.
3465 !(expected == &t_float && (stacktype == &t_number
3466 || stacktype == &t_number_bool))
3467 && need_type(stacktype, expected, TRUE, -1, 0, cctx,
3468 FALSE, FALSE) == FAIL)
3469 return FAIL;
3470 }
3471
3472 if (*cac->cac_op == '.')
3473 {
3474 if (generate_CONCAT(cctx, 2) == FAIL)
3475 return FAIL;
3476 }
3477 else if (*cac->cac_op == '+')
3478 {
3479 if (generate_add_instr(cctx,
3480 operator_type(lhs->lhs_member_type, stacktype),
3481 lhs->lhs_member_type, stacktype,
3482 EXPR_APPEND) == FAIL)
3483 return FAIL;
3484 }
3485 else if (generate_two_op(cctx, cac->cac_op) == FAIL)
3486 return FAIL;
3487
3488 return OK;
3489}
3490
3491/*
3492 * Generate the STORE and SETTYPE instructions for an assignment statement.
3493 */
3494 static int
3495compile_assign_generate_store(cctx_T *cctx, cac_T *cac)
3496{
3497 lhs_T *lhs = &cac->cac_lhs;
3498 int save_lnum;
3499
3500 // Use the line number of the assignment for store instruction.
3501 save_lnum = cctx->ctx_lnum;
3502 cctx->ctx_lnum = cac->cac_start_lnum - 1;
3503
3504 if (lhs->lhs_has_index)
3505 {
3506 // Use the info in "lhs" to store the value at the index in the
3507 // list, dict or object.
3508 if (compile_assign_unlet(cac->cac_var_start, &cac->cac_lhs,
3509 TRUE, cac->cac_rhs_type, cctx) == FAIL)
3510 {
3511 cctx->ctx_lnum = save_lnum;
3512 return FAIL;
3513 }
3514 }
3515 else
3516 {
3517 if (cac->cac_is_decl && cac->cac_cmdidx == CMD_const &&
3518 (lhs->lhs_dest == dest_script
3519 || lhs->lhs_dest == dest_script_v9
3520 || lhs->lhs_dest == dest_global
3521 || lhs->lhs_dest == dest_local))
3522 // ":const var": lock the value, but not referenced variables
3523 generate_LOCKCONST(cctx);
3524
3525 type_T *inferred_type = cac->cac_inferred_type;
3526
3527 if ((lhs->lhs_type->tt_type == VAR_DICT
3528 || lhs->lhs_type->tt_type == VAR_LIST)
3529 && lhs->lhs_type->tt_member != NULL
3530 && lhs->lhs_type->tt_member != &t_any
3531 && lhs->lhs_type->tt_member != &t_unknown)
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01003532 // Set the type in the list or dict, so that it can be
3533 // checked, also in legacy script.
3534 generate_SETTYPE(cctx, lhs->lhs_type);
3535 else if (lhs->lhs_type->tt_type == VAR_TUPLE
3536 && lhs->lhs_type->tt_argcount != 0)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003537 generate_SETTYPE(cctx, lhs->lhs_type);
3538 else if (inferred_type != NULL
3539 && (inferred_type->tt_type == VAR_DICT
3540 || inferred_type->tt_type == VAR_LIST)
3541 && inferred_type->tt_member != NULL
3542 && inferred_type->tt_member != &t_unknown
3543 && inferred_type->tt_member != &t_any)
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01003544 // Set the type in the list or dict, so that it can be
3545 // checked, also in legacy script.
3546 generate_SETTYPE(cctx, inferred_type);
3547 else if (inferred_type != NULL
3548 && inferred_type->tt_type == VAR_TUPLE
3549 && inferred_type->tt_argcount > 0)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003550 generate_SETTYPE(cctx, inferred_type);
3551
3552 if (!cac->cac_skip_store &&
3553 generate_store_lhs(cctx, &cac->cac_lhs,
3554 cac->cac_instr_count,
3555 cac->cac_is_decl) == FAIL)
3556 {
3557 cctx->ctx_lnum = save_lnum;
3558 return FAIL;
3559 }
3560 }
3561
3562 cctx->ctx_lnum = save_lnum;
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003563
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003564 return OK;
3565}
3566
3567/*
3568 * Process the variable(s) in an assignment statement
3569 */
3570 static int
3571compile_assign_process_variables(
3572 cctx_T *cctx,
3573 cac_T *cac,
3574 int cmdidx,
3575 int heredoc,
3576 int has_cmd,
3577 int has_argisset_prefix,
3578 int jump_instr_idx)
3579{
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003580 /*
3581 * Loop over variables in "[var, var] = expr".
3582 * For "name = expr" and "var name: type" this is done only once.
3583 */
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003584 for (cac->cac_var_idx = 0; cac->cac_var_idx == 0 ||
3585 cac->cac_var_idx < cac->cac_var_count; cac->cac_var_idx++)
3586 {
3587 if (cac->cac_var_start[0] == '_'
3588 && !eval_isnamec(cac->cac_var_start[1]))
3589 {
3590 // Ignore underscore in "[a, _, b] = list".
3591 if (cac->cac_var_count > 0)
3592 {
3593 cac->cac_var_start = skipwhite(cac->cac_var_start + 2);
3594 continue;
3595 }
3596 emsg(_(e_cannot_use_underscore_here));
3597 return FAIL;
3598 }
3599 vim_free(cac->cac_lhs.lhs_name);
3600
3601 /*
3602 * Figure out the LHS type and other properties.
3603 */
3604 if (compile_assign_lhs(cac->cac_var_start, &cac->cac_lhs, cmdidx,
3605 cac->cac_is_decl, heredoc, has_cmd,
3606 cac->cac_oplen, cctx) == FAIL)
3607 return FAIL;
3608
3609 // Compile the RHS expression
3610 if (heredoc)
3611 {
3612 SOURCING_LNUM = cac->cac_start_lnum;
3613 if (cac->cac_lhs.lhs_has_type
3614 && need_type(&t_list_string, cac->cac_lhs.lhs_type,
3615 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
3616 return FAIL;
3617 }
3618 else
3619 {
3620 if (compile_assign_rhs(cctx, cac) == FAIL)
3621 return FAIL;
3622 if (cac->cac_var_count == 0)
3623 cac->cac_var_end = cac->cac_nextc;
3624 }
3625
3626 // no need to parse more when skipping
3627 if (cctx->ctx_skip == SKIP_YES)
3628 break;
3629
3630 if (cac->cac_oplen > 0 && *cac->cac_op != '=')
3631 {
3632 if (compile_assign_compound_op(cctx, cac) == FAIL)
3633 return FAIL;
3634 }
3635
3636 // generate the store instructions
3637 if (compile_assign_generate_store(cctx, cac) == FAIL)
3638 return FAIL;
3639
3640 if (cac->cac_var_idx + 1 < cac->cac_var_count)
3641 cac->cac_var_start = skipwhite(cac->cac_lhs.lhs_end + 1);
3642
3643 if (has_argisset_prefix)
3644 {
3645 // set instruction index in JUMP_IF_ARG_SET to here
3646 isn_T *isn = ((isn_T *)cac->cac_instr->ga_data) + jump_instr_idx;
3647 isn->isn_arg.jumparg.jump_where = cac->cac_instr->ga_len;
3648 }
3649 }
3650
3651 return OK;
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003652}
3653
3654/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003655 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003656 * "let name"
3657 * "var name = expr"
3658 * "final name = expr"
3659 * "const name = expr"
3660 * "name = expr"
3661 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003662 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003663 * Return NULL for an error.
3664 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003665 */
3666 static char_u *
Bram Moolenaar65b0d162022-12-13 18:43:22 +00003667compile_assignment(
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003668 char_u *arg_start,
3669 exarg_T *eap,
3670 cmdidx_T cmdidx,
3671 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003672{
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003673 cac_T cac;
Bram Moolenaar65b0d162022-12-13 18:43:22 +00003674 char_u *arg = arg_start;
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003675 char_u *retstr = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676 int heredoc = FALSE;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003677 int jump_instr_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003678
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003679 compile_assign_context_init(&cac, cctx, cmdidx, arg);
3680
3681 jump_instr_idx = cac.cac_instr->ga_len;
3682
3683 // process object variable initialization in a new() constructor method
3684 int has_argisset_prefix = STRNCMP(arg, "ifargisset ", 11) == 0;
3685 if (has_argisset_prefix &&
3686 compile_assign_obj_new_arg(&arg, cctx) == FAIL)
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003687 goto theend;
Bram Moolenaar65b0d162022-12-13 18:43:22 +00003688
Bram Moolenaare1d12112022-03-05 11:37:48 +00003689 // Skip over the "varname" or "[varname, varname]" to get to any "=".
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003690 cac.cac_nextc = skip_var_list(arg, TRUE, &cac.cac_var_count,
3691 &cac.cac_semicolon, TRUE);
3692 if (cac.cac_nextc == NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003693 return *arg == '[' ? arg : NULL;
3694
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003695 if (compile_assign_process_operator(eap, arg, &cac, &heredoc,
3696 &retstr) == FAIL)
3697 return retstr;
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003698
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003699 // Compute the start of the assignment
3700 cac.cac_var_start = compile_assign_compute_start(arg, cac.cac_var_count);
3701
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003702 // Compute the end of the assignment
3703 cac.cac_var_end = compile_assign_compute_end(eap, cctx, &cac, heredoc);
3704 if (cac.cac_var_end == NULL)
3705 return NULL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003706
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003707 int has_cmd = cac.cac_var_start > eap->cmd;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003708
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003709 /* process the variable(s) */
3710 if (compile_assign_process_variables(cctx, &cac, cmdidx, heredoc,
3711 has_cmd, has_argisset_prefix,
3712 jump_instr_idx) == FAIL)
3713 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003714
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02003715 // For "[var, var] = expr" drop the "expr" value.
3716 // Also for "[var, var; _] = expr".
Yegappan Lakshmanane2038412024-12-14 19:59:24 +01003717 if (cctx->ctx_skip != SKIP_YES && cac.cac_var_count > 0 &&
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003718 (!cac.cac_semicolon || !cac.cac_did_generate_slice))
Bram Moolenaar9af78762020-06-16 11:34:42 +02003719 {
Bram Moolenaarec792292020-12-13 21:26:56 +01003720 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02003721 goto theend;
3722 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003723
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003724 retstr = skipwhite(cac.cac_var_end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003725
3726theend:
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003727 vim_free(cac.cac_lhs.lhs_name);
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003728 return retstr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003729}
3730
3731/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01003732 * Check for an assignment at "eap->cmd", compile it if found.
3733 * Return NOTDONE if there is none, FAIL for failure, OK if done.
3734 */
3735 static int
3736may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
3737{
3738 char_u *pskip;
3739 char_u *p;
3740
3741 // Assuming the command starts with a variable or function name,
3742 // find what follows.
3743 // Skip over "var.member", "var[idx]" and the like.
3744 // Also "&opt = val", "$ENV = val" and "@r = val".
3745 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
3746 ? eap->cmd + 1 : eap->cmd;
3747 p = to_name_end(pskip, TRUE);
3748 if (p > eap->cmd && *p != NUL)
3749 {
3750 char_u *var_end;
3751 int oplen;
3752 int heredoc;
3753
3754 if (eap->cmd[0] == '@')
3755 var_end = eap->cmd + 2;
3756 else
3757 var_end = find_name_end(pskip, NULL, NULL,
3758 FNE_CHECK_START | FNE_INCL_BR);
3759 oplen = assignment_len(skipwhite(var_end), &heredoc);
3760 if (oplen > 0)
3761 {
3762 size_t len = p - eap->cmd;
3763
3764 // Recognize an assignment if we recognize the variable
3765 // name:
Bram Moolenaar17126b12021-01-07 22:03:02 +01003766 // "&opt = expr"
3767 // "$ENV = expr"
3768 // "@r = expr"
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003769 // "g:var = expr"
3770 // "g:[key] = expr"
3771 // "local = expr" where "local" is a local var.
3772 // "script = expr" where "script" is a script-local var.
3773 // "import = expr" where "import" is an imported var
Bram Moolenaar17126b12021-01-07 22:03:02 +01003774 if (*eap->cmd == '&'
3775 || *eap->cmd == '$'
3776 || *eap->cmd == '@'
3777 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003778 || STRNCMP(eap->cmd, "g:[", 3) == 0
Bram Moolenaare0890d62021-02-17 14:52:14 +01003779 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01003780 {
3781 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
3782 if (*line == NULL || *line == eap->cmd)
3783 return FAIL;
3784 return OK;
3785 }
3786 }
3787 }
3788
Bram Moolenaar65b0d162022-12-13 18:43:22 +00003789 // might be "[var, var] = expr" or "ifargisset this.member = expr"
3790 if (*eap->cmd == '[' || STRNCMP(eap->cmd, "ifargisset ", 11) == 0)
Bram Moolenaar17126b12021-01-07 22:03:02 +01003791 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01003792 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
3793 if (*line == NULL)
3794 return FAIL;
3795 if (*line != eap->cmd)
3796 return OK;
3797 }
3798 return NOTDONE;
3799}
3800
Bram Moolenaar9a015112021-12-31 14:06:45 +00003801/*
3802 * Check if arguments of "ufunc" shadow variables in "cctx".
3803 * Return OK or FAIL.
3804 */
3805 static int
3806check_args_shadowing(ufunc_T *ufunc, cctx_T *cctx)
3807{
3808 int i;
3809 char_u *arg;
3810 int r = OK;
3811
3812 // Make sure arguments are not found when compiling a second time.
3813 ufunc->uf_args_visible = 0;
3814
3815 // Check for arguments shadowing variables from the context.
3816 for (i = 0; i < ufunc->uf_args.ga_len; ++i)
3817 {
3818 arg = ((char_u **)(ufunc->uf_args.ga_data))[i];
Bram Moolenaardce24412022-02-08 20:35:30 +00003819 if (check_defined(arg, STRLEN(arg), cctx, NULL, TRUE) == FAIL)
Bram Moolenaar9a015112021-12-31 14:06:45 +00003820 {
3821 r = FAIL;
3822 break;
3823 }
3824 }
3825 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
3826 return r;
3827}
3828
Bram Moolenaarfcb86b02022-10-07 22:46:24 +01003829#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar139575d2022-03-15 19:29:30 +00003830/*
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003831 * Get a count before a command. Can only be a number.
3832 * Returns zero if there is no count.
3833 * Returns -1 if there is something wrong.
3834 */
3835 static long
3836get_cmd_count(char_u *line, exarg_T *eap)
3837{
3838 char_u *p;
3839
3840 // skip over colons and white space
3841 for (p = line; *p == ':' || VIM_ISWHITE(*p); ++p)
3842 ;
Keith Thompson184f71c2024-01-04 21:19:04 +01003843 if (!SAFE_isdigit(*p))
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003844 {
Bram Moolenaar2435adf2022-10-21 12:05:46 +01003845 // The command or modifiers must be following. Assume a lower case
3846 // character means there is a modifier.
3847 if (p < eap->cmd && !vim_islower(*p))
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003848 {
3849 emsg(_(e_invalid_range));
3850 return -1;
3851 }
3852 return 0;
3853 }
3854 return atol((char *)p);
3855}
Bram Moolenaarfcb86b02022-10-07 22:46:24 +01003856#endif
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003857
3858/*
Bram Moolenaar139575d2022-03-15 19:29:30 +00003859 * Get the compilation type that should be used for "ufunc".
3860 * Keep in sync with INSTRUCTIONS().
3861 */
3862 compiletype_T
3863get_compile_type(ufunc_T *ufunc)
3864{
3865 // Update uf_has_breakpoint if needed.
3866 update_has_breakpoint(ufunc);
3867
3868 if (debug_break_level > 0 || may_break_in_function(ufunc))
3869 return CT_DEBUG;
3870#ifdef FEAT_PROFILE
3871 if (do_profiling == PROF_YES)
3872 {
Ernie Rael21d32122023-09-02 15:09:18 +02003873 if (!ufunc->uf_profiling && has_profiling(FALSE, ufunc->uf_name, NULL,
3874 &ufunc->uf_hash))
Bram Moolenaar139575d2022-03-15 19:29:30 +00003875 func_do_profile(ufunc);
3876 if (ufunc->uf_profiling)
3877 return CT_PROFILE;
3878 }
3879#endif
3880 return CT_NONE;
3881}
3882
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01003883/*
3884 * Free the compiled instructions saved for a def function. This is used when
3885 * compiling a def function and the function was compiled before.
3886 * The index is reused.
3887 */
3888 static void
3889clear_def_function(ufunc_T *ufunc, compiletype_T compile_type)
3890{
3891 isn_T *instr_dest = NULL;
3892 dfunc_T *dfunc;
3893
3894 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
3895
3896 switch (compile_type)
3897 {
3898 case CT_PROFILE:
3899#ifdef FEAT_PROFILE
3900 instr_dest = dfunc->df_instr_prof; break;
3901#endif
3902 case CT_NONE: instr_dest = dfunc->df_instr; break;
3903 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
3904 }
3905
3906 if (instr_dest != NULL)
3907 // Was compiled in this mode before: Free old instructions.
3908 delete_def_function_contents(dfunc, FALSE);
3909
3910 ga_clear_strings(&dfunc->df_var_names);
3911 dfunc->df_defer_var_idx = 0;
3912}
Bram Moolenaar7b829262021-10-13 15:04:34 +01003913
3914/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02003915 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003916 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02003917 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02003918 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02003919add_def_function(ufunc_T *ufunc)
3920{
3921 dfunc_T *dfunc;
3922
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003923 if (def_functions.ga_len == 0)
3924 {
3925 // The first position is not used, so that a zero uf_dfunc_idx means it
3926 // wasn't set.
Bram Moolenaar35578162021-08-02 19:10:38 +02003927 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003928 return FAIL;
3929 ++def_functions.ga_len;
3930 }
3931
Bram Moolenaar09689a02020-05-09 22:50:08 +02003932 // Add the function to "def_functions".
Bram Moolenaar35578162021-08-02 19:10:38 +02003933 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar09689a02020-05-09 22:50:08 +02003934 return FAIL;
3935 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
3936 CLEAR_POINTER(dfunc);
3937 dfunc->df_idx = def_functions.ga_len;
3938 ufunc->uf_dfunc_idx = dfunc->df_idx;
3939 dfunc->df_ufunc = ufunc;
John Marriottb32800f2025-02-01 15:25:34 +01003940 dfunc->df_name = vim_strnsave(ufunc->uf_name, ufunc->uf_namelen);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02003941 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003942 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02003943 ++def_functions.ga_len;
3944 return OK;
3945}
3946
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01003947 static int
3948compile_dfunc_ufunc_init(
3949 ufunc_T *ufunc,
3950 cctx_T *outer_cctx,
3951 compiletype_T compile_type,
3952 int *new_def_function)
3953{
3954 // When using a function that was compiled before: Free old instructions.
3955 // The index is reused. Otherwise add a new entry in "def_functions".
3956 if (ufunc->uf_dfunc_idx > 0)
3957 clear_def_function(ufunc, compile_type);
3958 else
3959 {
3960 if (add_def_function(ufunc) == FAIL)
3961 return FAIL;
3962
3963 *new_def_function = TRUE;
3964 }
3965
3966 if ((ufunc->uf_flags & FC_CLOSURE) && outer_cctx == NULL)
3967 {
3968 semsg(_(e_compiling_closure_without_context_str),
3969 printable_func_name(ufunc));
3970 return FAIL;
3971 }
3972
3973 ufunc->uf_def_status = UF_COMPILING;
3974
3975 return OK;
3976}
3977
3978/*
3979 * Initialize the compilation context for compiling a def function.
3980 */
3981 static void
3982compile_dfunc_cctx_init(
3983 cctx_T *cctx,
3984 cctx_T *outer_cctx,
3985 ufunc_T *ufunc,
3986 compiletype_T compile_type)
3987{
3988 CLEAR_FIELD(*cctx);
3989
3990 cctx->ctx_compile_type = compile_type;
3991 cctx->ctx_ufunc = ufunc;
3992 cctx->ctx_lnum = -1;
3993 cctx->ctx_outer = outer_cctx;
3994 ga_init2(&cctx->ctx_locals, sizeof(lvar_T), 10);
3995 // Each entry on the type stack consists of two type pointers.
3996 ga_init2(&cctx->ctx_type_stack, sizeof(type2_T), 50);
3997 cctx->ctx_type_list = &ufunc->uf_type_list;
3998 ga_init2(&cctx->ctx_instr, sizeof(isn_T), 50);
3999}
4000
Bram Moolenaar09689a02020-05-09 22:50:08 +02004001/*
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004002 * For an object constructor, generate instruction to setup "this" (the first
4003 * local variable) and to initialize the object variables.
4004 */
4005 static int
4006obj_constructor_prologue(ufunc_T *ufunc, cctx_T *cctx)
4007{
4008 generate_CONSTRUCT(cctx, ufunc->uf_class);
4009
4010 for (int i = 0; i < ufunc->uf_class->class_obj_member_count; ++i)
4011 {
4012 ocmember_T *m = &ufunc->uf_class->class_obj_members[i];
4013
4014 if (i < 2 && IS_ENUM(ufunc->uf_class))
4015 // The first two object variables in an enum are the name
4016 // and the ordinal. These are set by the ISN_CONSTRUCT
4017 // instruction. So don't generate instructions to set
4018 // these variables.
4019 continue;
4020
4021 if (m->ocm_init != NULL)
4022 {
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +01004023 char_u *expr = m->ocm_init;
4024 sctx_T save_current_sctx;
4025 int change_sctx = FALSE;
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004026
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +01004027 // If the member variable initialization script context is
4028 // different from the current script context, then change it.
4029 if (current_sctx.sc_sid != m->ocm_init_sctx.sc_sid)
4030 change_sctx = TRUE;
4031
4032 if (change_sctx)
4033 {
4034 // generate an instruction to change the script context to the
4035 // member variable initialization script context.
4036 save_current_sctx = current_sctx;
4037 current_sctx = m->ocm_init_sctx;
4038 generate_SCRIPTCTX_SET(cctx, current_sctx);
4039 }
4040
4041 int r = compile_expr0(&expr, cctx);
4042
4043 if (change_sctx)
4044 {
4045 // restore the previous script context
4046 current_sctx = save_current_sctx;
4047 generate_SCRIPTCTX_SET(cctx, current_sctx);
4048 }
4049
4050 if (r == FAIL)
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004051 return FAIL;
4052
4053 if (!ends_excmd2(m->ocm_init, expr))
4054 {
4055 semsg(_(e_trailing_characters_str), expr);
4056 return FAIL;
4057 }
4058
4059 type_T *type = get_type_on_stack(cctx, 0);
4060 if (m->ocm_type->tt_type == VAR_ANY
4061 && !(m->ocm_flags & OCMFLAG_HAS_TYPE)
4062 && type->tt_type != VAR_SPECIAL)
4063 {
4064 // If the member variable type is not yet set, then use
4065 // the initialization expression type.
4066 m->ocm_type = type;
4067 }
LemonBoyf4af3312024-07-04 13:43:12 +02004068 else
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004069 {
4070 // The type of the member initialization expression is
4071 // determined at run time. Add a runtime type check.
4072 where_T where = WHERE_INIT;
4073 where.wt_kind = WT_MEMBER;
4074 where.wt_func_name = (char *)m->ocm_name;
4075 if (need_type_where(type, m->ocm_type, FALSE, -1,
4076 where, cctx, FALSE, FALSE) == FAIL)
4077 return FAIL;
4078 }
4079 }
4080 else
4081 push_default_value(cctx, m->ocm_type->tt_type, FALSE, NULL);
4082
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01004083 if (((m->ocm_type->tt_type == VAR_DICT
4084 || m->ocm_type->tt_type == VAR_LIST)
4085 && m->ocm_type->tt_member != NULL
4086 && m->ocm_type->tt_member != &t_any
4087 && m->ocm_type->tt_member != &t_unknown)
4088 || (m->ocm_type->tt_type == VAR_TUPLE
4089 && m->ocm_type->tt_argcount > 0))
4090 // Set the type in the list, tuple or dict, so that it can be
4091 // checked, also in legacy script.
LemonBoyf4af3312024-07-04 13:43:12 +02004092 generate_SETTYPE(cctx, m->ocm_type);
4093
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004094 generate_STORE_THIS(cctx, i);
4095 }
4096
4097 return OK;
4098}
4099
4100/*
4101 * For an object method and an constructor, generate instruction to setup
4102 * "this" (the first local variable). For a constructor, generate instructions
4103 * to initialize the object variables.
4104 */
4105 static int
4106obj_method_prologue(ufunc_T *ufunc, cctx_T *cctx)
4107{
4108 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
4109
4110 if (GA_GROW_FAILS(&dfunc->df_var_names, 1))
4111 return FAIL;
4112
4113 ((char_u **)dfunc->df_var_names.ga_data)[0] =
4114 vim_strsave((char_u *)"this");
4115 ++dfunc->df_var_names.ga_len;
4116
4117 // In the constructor allocate memory for the object and initialize the
4118 // object members.
4119 if (IS_CONSTRUCTOR_METHOD(ufunc))
4120 return obj_constructor_prologue(ufunc, cctx);
4121
4122 return OK;
4123}
4124
4125/*
4126 * Produce instructions for the default values of optional arguments.
4127 */
4128 static int
4129compile_def_function_default_args(
4130 ufunc_T *ufunc,
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004131 garray_T *instr,
4132 cctx_T *cctx)
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004133{
4134 int count = ufunc->uf_def_args.ga_len;
4135 int first_def_arg = ufunc->uf_args.ga_len - count;
4136 int i;
4137 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
4138 int did_set_arg_type = FALSE;
4139
4140 // Produce instructions for the default values of optional arguments.
4141 SOURCING_LNUM = 0; // line number unknown
4142 for (i = 0; i < count; ++i)
4143 {
4144 char_u *arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
4145 if (STRCMP(arg, "v:none") == 0)
4146 // "arg = v:none" means the argument is optional without
4147 // setting a value when the argument is missing.
4148 continue;
4149
4150 type_T *val_type;
4151 int arg_idx = first_def_arg + i;
4152 where_T where = WHERE_INIT;
4153 int jump_instr_idx = instr->ga_len;
4154 isn_T *isn;
4155
4156 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
4157 if (generate_JUMP_IF_ARG(cctx, ISN_JUMP_IF_ARG_SET,
4158 i - count - off) == FAIL)
4159 return FAIL;
4160
4161 // Make sure later arguments are not found.
4162 ufunc->uf_args_visible = arg_idx;
4163
4164 int r = compile_expr0(&arg, cctx);
4165 if (r == FAIL)
4166 return FAIL;
4167
4168 // If no type specified use the type of the default value.
4169 // Otherwise check that the default value type matches the
4170 // specified type.
4171 val_type = get_type_on_stack(cctx, 0);
4172 where.wt_index = arg_idx + 1;
4173 where.wt_kind = WT_ARGUMENT;
4174 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
4175 {
4176 did_set_arg_type = TRUE;
4177 ufunc->uf_arg_types[arg_idx] = val_type;
4178 }
4179 else if (need_type_where(val_type, ufunc->uf_arg_types[arg_idx],
4180 FALSE, -1, where, cctx, FALSE, FALSE) == FAIL)
4181 return FAIL;
4182
4183 if (generate_STORE(cctx, ISN_STORE, i - count - off, NULL) == FAIL)
4184 return FAIL;
4185
4186 // set instruction index in JUMP_IF_ARG_SET to here
4187 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
4188 isn->isn_arg.jumparg.jump_where = instr->ga_len;
4189 }
4190
4191 if (did_set_arg_type)
4192 set_function_type(ufunc);
4193
4194 return OK;
4195}
4196
4197/*
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004198 * Compile def function body. Loop over all the lines in the function and
4199 * generate instructions.
4200 */
4201 static int
4202compile_def_function_body(
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004203 int last_func_lnum,
4204 int check_return_type,
4205 garray_T *lines_to_free,
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004206 char **errormsg,
4207 cctx_T *cctx)
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004208{
4209 char_u *line = NULL;
4210 char_u *p;
4211 int did_emsg_before = did_emsg;
4212#ifdef FEAT_PROFILE
4213 int prof_lnum = -1;
4214#endif
4215 int debug_lnum = -1;
4216
4217 for (;;)
4218 {
4219 exarg_T ea;
4220 int starts_with_colon = FALSE;
4221 char_u *cmd;
4222 cmdmod_T local_cmdmod;
4223
4224 // Bail out on the first error to avoid a flood of errors and report
4225 // the right line number when inside try/catch.
4226 if (did_emsg_before != did_emsg)
4227 return FAIL;
4228
4229 if (line != NULL && *line == '|')
4230 // the line continues after a '|'
4231 ++line;
4232 else if (line != NULL && *skipwhite(line) != NUL
4233 && !(*line == '#' && (line == cctx->ctx_line_start
4234 || VIM_ISWHITE(line[-1]))))
4235 {
4236 semsg(_(e_trailing_characters_str), line);
4237 return FAIL;
4238 }
4239 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
4240 return FAIL;
4241 else
4242 {
4243 line = next_line_from_context(cctx, FALSE);
4244 if (cctx->ctx_lnum >= last_func_lnum)
4245 {
4246 // beyond the last line
4247#ifdef FEAT_PROFILE
4248 if (cctx->ctx_skip != SKIP_YES)
4249 may_generate_prof_end(cctx, prof_lnum);
4250#endif
4251 break;
4252 }
4253 // Make a copy, splitting off nextcmd and removing trailing spaces
4254 // may change it.
4255 if (line != NULL)
4256 {
4257 line = vim_strsave(line);
4258 if (ga_add_string(lines_to_free, line) == FAIL)
4259 return FAIL;
4260 }
4261 }
4262
4263 CLEAR_FIELD(ea);
4264 ea.cmdlinep = &line;
4265 ea.cmd = skipwhite(line);
4266 ea.skip = cctx->ctx_skip == SKIP_YES;
4267
4268 if (*ea.cmd == '#')
4269 {
4270 // "#" starts a comment, but "#{" is an error
4271 if (vim9_bad_comment(ea.cmd))
4272 return FAIL;
4273 line = (char_u *)"";
4274 continue;
4275 }
4276
4277#ifdef FEAT_PROFILE
4278 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_lnum != prof_lnum
4279 && cctx->ctx_skip != SKIP_YES)
4280 {
4281 may_generate_prof_end(cctx, prof_lnum);
4282
4283 prof_lnum = cctx->ctx_lnum;
4284 generate_instr(cctx, ISN_PROF_START);
4285 }
4286#endif
4287 if (cctx->ctx_compile_type == CT_DEBUG && cctx->ctx_lnum != debug_lnum
4288 && cctx->ctx_skip != SKIP_YES)
4289 {
4290 debug_lnum = cctx->ctx_lnum;
4291 generate_instr_debug(cctx);
4292 }
4293 cctx->ctx_prev_lnum = cctx->ctx_lnum + 1;
4294
4295 // Some things can be recognized by the first character.
4296 switch (*ea.cmd)
4297 {
4298 case '}':
4299 {
4300 // "}" ends a block scope
4301 scopetype_T stype = cctx->ctx_scope == NULL
4302 ? NO_SCOPE : cctx->ctx_scope->se_type;
4303
4304 if (stype == BLOCK_SCOPE)
4305 {
4306 compile_endblock(cctx);
4307 line = ea.cmd;
4308 }
4309 else
4310 {
4311 emsg(_(e_using_rcurly_outside_if_block_scope));
4312 return FAIL;
4313 }
4314 if (line != NULL)
4315 line = skipwhite(ea.cmd + 1);
4316 continue;
4317 }
4318
4319 case '{':
4320 // "{" starts a block scope
4321 // "{'a': 1}->func() is something else
4322 if (ends_excmd(*skipwhite(ea.cmd + 1)))
4323 {
4324 line = compile_block(ea.cmd, cctx);
4325 continue;
4326 }
4327 break;
4328 }
4329
4330 /*
4331 * COMMAND MODIFIERS
4332 */
4333 cctx->ctx_has_cmdmod = FALSE;
4334 if (parse_command_modifiers(&ea, errormsg, &local_cmdmod, FALSE)
4335 == FAIL)
4336 return FAIL;
4337 generate_cmdmods(cctx, &local_cmdmod);
4338 undo_cmdmod(&local_cmdmod);
4339
4340 // Check if there was a colon after the last command modifier or before
4341 // the current position.
4342 for (p = ea.cmd; p >= line; --p)
4343 {
4344 if (*p == ':')
4345 starts_with_colon = TRUE;
4346 if (p < ea.cmd && !VIM_ISWHITE(*p))
4347 break;
4348 }
4349
4350 // Skip ":call" to get to the function name, unless using :legacy
4351 p = ea.cmd;
4352 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
4353 {
4354 if (checkforcmd(&ea.cmd, "call", 3))
4355 {
4356 if (*ea.cmd == '(')
4357 // not for "call()"
4358 ea.cmd = p;
4359 else
4360 ea.cmd = skipwhite(ea.cmd);
4361 }
4362
4363 if (!starts_with_colon)
4364 {
4365 int assign;
4366
4367 // Check for assignment after command modifiers.
4368 assign = may_compile_assignment(&ea, &line, cctx);
4369 if (assign == OK)
4370 goto nextline;
4371 if (assign == FAIL)
4372 return FAIL;
4373 }
4374 }
4375
4376 /*
4377 * COMMAND after range
4378 * 'text'->func() should not be confused with 'a mark
4379 * 0z1234->func() should not be confused with a zero line number
4380 * "++nr" and "--nr" are eval commands
4381 * in "$ENV->func()" the "$" is not a range
4382 * "123->func()" is a method call
4383 */
4384 cmd = ea.cmd;
4385 if ((*cmd != '$' || starts_with_colon)
4386 && (starts_with_colon
4387 || !(*cmd == '\''
4388 || (cmd[0] == '0' && cmd[1] == 'z')
4389 || (cmd[0] != NUL && cmd[0] == cmd[1]
4390 && (*cmd == '+' || *cmd == '-'))
4391 || number_method(cmd))))
4392 {
4393 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
4394 if (ea.cmd > cmd)
4395 {
4396 if (!starts_with_colon
4397 && !(local_cmdmod.cmod_flags & CMOD_LEGACY))
4398 {
4399 semsg(_(e_colon_required_before_range_str), cmd);
4400 return FAIL;
4401 }
4402 ea.addr_count = 1;
4403 if (ends_excmd2(line, ea.cmd))
4404 {
4405 // A range without a command: jump to the line.
4406 generate_EXEC(cctx, ISN_EXECRANGE,
4407 vim_strnsave(cmd, ea.cmd - cmd));
4408 line = ea.cmd;
4409 goto nextline;
4410 }
4411 }
4412 }
4413 p = find_ex_command(&ea, NULL,
4414 starts_with_colon || (local_cmdmod.cmod_flags & CMOD_LEGACY)
4415 ? NULL : item_exists, cctx);
4416
4417 if (p == NULL)
4418 {
4419 if (cctx->ctx_skip != SKIP_YES)
4420 semsg(_(e_ambiguous_use_of_user_defined_command_str), ea.cmd);
4421 return FAIL;
4422 }
4423
4424 // When using ":legacy cmd" always use compile_exec().
4425 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
4426 {
4427 char_u *start = ea.cmd;
4428
4429 switch (ea.cmdidx)
4430 {
4431 case CMD_if:
4432 case CMD_elseif:
4433 case CMD_else:
4434 case CMD_endif:
4435 case CMD_for:
4436 case CMD_endfor:
4437 case CMD_continue:
4438 case CMD_break:
4439 case CMD_while:
4440 case CMD_endwhile:
4441 case CMD_try:
4442 case CMD_catch:
4443 case CMD_finally:
4444 case CMD_endtry:
4445 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
4446 return FAIL;
4447 default: break;
4448 }
4449
4450 // ":legacy return expr" needs to be handled differently.
4451 if (checkforcmd(&start, "return", 4))
4452 ea.cmdidx = CMD_return;
4453 else
4454 ea.cmdidx = CMD_legacy;
4455 }
4456
4457 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
4458 {
4459 // "eval" is used for "val->func()" and "var" for "var = val", then
4460 // "p" is equal to "ea.cmd" for a valid command.
4461 if (ea.cmdidx == CMD_eval || ea.cmdidx == CMD_var)
4462 ;
4463 else if (cctx->ctx_skip == SKIP_YES)
4464 {
4465 line += STRLEN(line);
4466 goto nextline;
4467 }
4468 else
4469 {
4470 semsg(_(e_command_not_recognized_str), ea.cmd);
4471 return FAIL;
4472 }
4473 }
4474
4475 if ((cctx->ctx_had_return || cctx->ctx_had_throw)
4476 && ea.cmdidx != CMD_elseif
4477 && ea.cmdidx != CMD_else
4478 && ea.cmdidx != CMD_endif
4479 && ea.cmdidx != CMD_endfor
4480 && ea.cmdidx != CMD_endwhile
4481 && ea.cmdidx != CMD_catch
4482 && ea.cmdidx != CMD_finally
4483 && ea.cmdidx != CMD_endtry
4484 && !ignore_unreachable_code_for_testing)
4485 {
4486 semsg(_(e_unreachable_code_after_str),
4487 cctx->ctx_had_return ? "return" : "throw");
4488 return FAIL;
4489 }
Yegappan Lakshmananab9a8942024-12-30 09:56:34 +01004490
4491 // When processing the end of an if-else block, don't clear the
4492 // "ctx_had_throw" flag. If an if-else block ends in a "throw"
4493 // statement, then it is considered to end in a "return" statement.
4494 // The "ctx_had_throw" is cleared immediately after processing the
4495 // if-else block ending statement.
4496 // Otherwise, clear the "had_throw" flag.
4497 if (ea.cmdidx != CMD_else && ea.cmdidx != CMD_elseif
4498 && ea.cmdidx != CMD_endif)
4499 cctx->ctx_had_throw = FALSE;
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004500
4501 p = skipwhite(p);
4502 if (ea.cmdidx != CMD_SIZE
4503 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
4504 {
4505 if (ea.cmdidx >= 0)
4506 ea.argt = excmd_get_argt(ea.cmdidx);
4507 if ((ea.argt & EX_BANG) && *p == '!')
4508 {
4509 ea.forceit = TRUE;
4510 p = skipwhite(p + 1);
4511 }
4512 if ((ea.argt & EX_RANGE) == 0 && ea.addr_count > 0)
4513 {
4514 emsg(_(e_no_range_allowed));
4515 return FAIL;
4516 }
4517 }
4518
4519 switch (ea.cmdidx)
4520 {
4521 case CMD_def:
4522 case CMD_function:
4523 ea.arg = p;
4524 line = compile_nested_function(&ea, cctx, lines_to_free);
4525 break;
4526
4527 case CMD_return:
4528 line = compile_return(p, check_return_type,
4529 local_cmdmod.cmod_flags & CMOD_LEGACY, cctx);
4530 cctx->ctx_had_return = TRUE;
4531 break;
4532
4533 case CMD_let:
4534 emsg(_(e_cannot_use_let_in_vim9_script));
4535 break;
4536 case CMD_var:
4537 case CMD_final:
4538 case CMD_const:
4539 case CMD_increment:
4540 case CMD_decrement:
4541 line = compile_assignment(p, &ea, ea.cmdidx, cctx);
4542 if (line == p)
4543 {
4544 emsg(_(e_invalid_assignment));
4545 line = NULL;
4546 }
4547 break;
4548
4549 case CMD_unlet:
4550 case CMD_unlockvar:
4551 case CMD_lockvar:
4552 line = compile_unletlock(p, &ea, cctx);
4553 break;
4554
4555 case CMD_import:
4556 emsg(_(e_import_can_only_be_used_in_script));
4557 line = NULL;
4558 break;
4559
4560 case CMD_if:
4561 line = compile_if(p, cctx);
4562 break;
4563 case CMD_elseif:
4564 line = compile_elseif(p, cctx);
4565 cctx->ctx_had_return = FALSE;
Yegappan Lakshmananab9a8942024-12-30 09:56:34 +01004566 cctx->ctx_had_throw = FALSE;
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004567 break;
4568 case CMD_else:
4569 line = compile_else(p, cctx);
4570 cctx->ctx_had_return = FALSE;
Yegappan Lakshmananab9a8942024-12-30 09:56:34 +01004571 cctx->ctx_had_throw = FALSE;
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004572 break;
4573 case CMD_endif:
4574 line = compile_endif(p, cctx);
Yegappan Lakshmananab9a8942024-12-30 09:56:34 +01004575 cctx->ctx_had_throw = FALSE;
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004576 break;
4577
4578 case CMD_while:
4579 line = compile_while(p, cctx);
4580 break;
4581 case CMD_endwhile:
4582 line = compile_endwhile(p, cctx);
4583 cctx->ctx_had_return = FALSE;
4584 break;
4585
4586 case CMD_for:
4587 line = compile_for(p, cctx);
4588 break;
4589 case CMD_endfor:
4590 line = compile_endfor(p, cctx);
4591 cctx->ctx_had_return = FALSE;
4592 break;
4593 case CMD_continue:
4594 line = compile_continue(p, cctx);
4595 break;
4596 case CMD_break:
4597 line = compile_break(p, cctx);
4598 break;
4599
4600 case CMD_try:
4601 line = compile_try(p, cctx);
4602 break;
4603 case CMD_catch:
4604 line = compile_catch(p, cctx);
4605 cctx->ctx_had_return = FALSE;
4606 break;
4607 case CMD_finally:
4608 line = compile_finally(p, cctx);
4609 cctx->ctx_had_return = FALSE;
4610 break;
4611 case CMD_endtry:
4612 line = compile_endtry(p, cctx);
4613 break;
4614 case CMD_throw:
4615 line = compile_throw(p, cctx);
4616 cctx->ctx_had_throw = TRUE;
4617 break;
4618
4619 case CMD_eval:
4620 line = compile_eval(p, cctx);
4621 break;
4622
4623 case CMD_defer:
4624 line = compile_defer(p, cctx);
4625 break;
4626
4627#ifdef HAS_MESSAGE_WINDOW
4628 case CMD_echowindow:
4629 {
4630 long cmd_count = get_cmd_count(line, &ea);
4631 if (cmd_count < 0)
4632 line = NULL;
4633 else
4634 line = compile_mult_expr(p, ea.cmdidx,
4635 cmd_count, cctx);
4636 }
4637 break;
4638#endif
4639 case CMD_echo:
4640 case CMD_echon:
4641 case CMD_echoconsole:
4642 case CMD_echoerr:
4643 case CMD_echomsg:
4644 case CMD_execute:
4645 line = compile_mult_expr(p, ea.cmdidx, 0, cctx);
4646 break;
4647
4648 case CMD_put:
4649 ea.cmd = cmd;
64-bitmane08f10a2025-03-18 22:14:34 +01004650 line = compile_put(p, &ea, cctx, FALSE);
4651 break;
4652
4653 case CMD_iput:
4654 ea.cmd = cmd;
4655 line = compile_put(p, &ea, cctx, TRUE);
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004656 break;
4657
4658 case CMD_substitute:
4659 if (check_global_and_subst(ea.cmd, p) == FAIL)
4660 return FAIL;
4661 if (cctx->ctx_skip == SKIP_YES)
4662 line = (char_u *)"";
4663 else
4664 {
4665 ea.arg = p;
4666 line = compile_substitute(line, &ea, cctx);
4667 }
4668 break;
4669
4670 case CMD_redir:
4671 ea.arg = p;
4672 line = compile_redir(line, &ea, cctx);
4673 break;
4674
4675 case CMD_cexpr:
4676 case CMD_lexpr:
4677 case CMD_caddexpr:
4678 case CMD_laddexpr:
4679 case CMD_cgetexpr:
4680 case CMD_lgetexpr:
4681#ifdef FEAT_QUICKFIX
4682 ea.arg = p;
4683 line = compile_cexpr(line, &ea, cctx);
4684#else
4685 ex_ni(&ea);
4686 line = NULL;
4687#endif
4688 break;
4689
4690 case CMD_append:
4691 case CMD_change:
4692 case CMD_insert:
4693 case CMD_k:
4694 case CMD_t:
4695 case CMD_xit:
4696 not_in_vim9(&ea);
4697 return FAIL;
4698
4699 case CMD_SIZE:
4700 if (cctx->ctx_skip != SKIP_YES)
4701 {
4702 semsg(_(e_invalid_command_str), ea.cmd);
4703 return FAIL;
4704 }
4705 // We don't check for a next command here.
4706 line = (char_u *)"";
4707 break;
4708
4709 case CMD_lua:
4710 case CMD_mzscheme:
4711 case CMD_perl:
4712 case CMD_py3:
4713 case CMD_python3:
4714 case CMD_python:
4715 case CMD_pythonx:
4716 case CMD_ruby:
4717 case CMD_tcl:
4718 ea.arg = p;
4719 if (vim_strchr(line, '\n') == NULL)
4720 line = compile_exec(line, &ea, cctx);
4721 else
4722 // heredoc lines have been concatenated with NL
4723 // characters in get_function_body()
4724 line = compile_script(line, cctx);
4725 break;
4726
4727 case CMD_vim9script:
4728 if (cctx->ctx_skip != SKIP_YES)
4729 {
4730 emsg(_(e_vim9script_can_only_be_used_in_script));
4731 return FAIL;
4732 }
4733 line = (char_u *)"";
4734 break;
4735
4736 case CMD_class:
4737 emsg(_(e_class_can_only_be_used_in_script));
4738 return FAIL;
4739
4740 case CMD_type:
4741 emsg(_(e_type_can_only_be_used_in_script));
4742 return FAIL;
4743
4744 case CMD_global:
4745 if (check_global_and_subst(ea.cmd, p) == FAIL)
4746 return FAIL;
4747 // FALLTHROUGH
4748 default:
4749 // Not recognized, execute with do_cmdline_cmd().
4750 ea.arg = p;
4751 line = compile_exec(line, &ea, cctx);
4752 break;
4753 }
4754nextline:
4755 if (line == NULL)
4756 return FAIL;
4757 line = skipwhite(line);
4758
4759 // Undo any command modifiers.
4760 generate_undo_cmdmods(cctx);
4761
4762 if (cctx->ctx_type_stack.ga_len < 0)
4763 {
4764 iemsg("Type stack underflow");
4765 return FAIL;
4766 }
4767 } // END of the loop over all the function body lines.
4768
4769 return OK;
4770}
4771
4772/*
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004773 * Returns TRUE if the end of a scope (if, while, for, block) is missing.
4774 * Called after compiling a def function body.
4775 */
4776 static int
4777compile_dfunc_scope_end_missing(cctx_T *cctx)
4778{
4779 if (cctx->ctx_scope == NULL)
4780 return FALSE;
4781
4782 if (cctx->ctx_scope->se_type == IF_SCOPE)
4783 emsg(_(e_missing_endif));
4784 else if (cctx->ctx_scope->se_type == WHILE_SCOPE)
4785 emsg(_(e_missing_endwhile));
4786 else if (cctx->ctx_scope->se_type == FOR_SCOPE)
4787 emsg(_(e_missing_endfor));
4788 else
4789 emsg(_(e_missing_rcurly));
4790
4791 return TRUE;
4792}
4793
4794/*
Yegappan Lakshmananab9a8942024-12-30 09:56:34 +01004795 * When compiling a def function, if it doesn't have an explicit return
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004796 * statement, then generate a default return instruction. For an object
4797 * constructor, return the object.
4798 */
4799 static int
4800compile_dfunc_generate_default_return(ufunc_T *ufunc, cctx_T *cctx)
4801{
4802 // TODO: if a function ends in "throw" but there was a return elsewhere we
4803 // should not assume the return type is "void".
4804 if (cctx->ctx_had_return || cctx->ctx_had_throw)
4805 return OK;
4806
4807 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
4808 ufunc->uf_ret_type = &t_void;
4809 else if (ufunc->uf_ret_type->tt_type != VAR_VOID
4810 && !IS_CONSTRUCTOR_METHOD(ufunc))
4811 {
4812 emsg(_(e_missing_return_statement));
4813 return FAIL;
4814 }
4815
4816 // Return void if there is no return at the end.
4817 // For a constructor return the object.
4818 if (IS_CONSTRUCTOR_METHOD(ufunc))
4819 {
4820 generate_instr(cctx, ISN_RETURN_OBJECT);
4821 ufunc->uf_ret_type = &ufunc->uf_class->class_object_type;
4822 }
4823 else
4824 generate_instr(cctx, ISN_RETURN_VOID);
4825
4826 return OK;
4827}
4828
4829/*
4830 * Perform the chores after successfully compiling a def function.
4831 */
4832 static void
4833compile_dfunc_epilogue(
4834 cctx_T *outer_cctx,
4835 ufunc_T *ufunc,
4836 garray_T *instr,
4837 cctx_T *cctx)
4838{
4839 dfunc_T *dfunc;
4840
4841 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
4842 dfunc->df_deleted = FALSE;
4843 dfunc->df_script_seq = current_sctx.sc_seq;
4844
4845#ifdef FEAT_PROFILE
4846 if (cctx->ctx_compile_type == CT_PROFILE)
4847 {
4848 dfunc->df_instr_prof = instr->ga_data;
4849 dfunc->df_instr_prof_count = instr->ga_len;
4850 }
4851 else
4852#endif
4853 if (cctx->ctx_compile_type == CT_DEBUG)
4854 {
4855 dfunc->df_instr_debug = instr->ga_data;
4856 dfunc->df_instr_debug_count = instr->ga_len;
4857 }
4858 else
4859 {
4860 dfunc->df_instr = instr->ga_data;
4861 dfunc->df_instr_count = instr->ga_len;
4862 }
4863 dfunc->df_varcount = dfunc->df_var_names.ga_len;
4864 dfunc->df_has_closure = cctx->ctx_has_closure;
4865
4866 if (cctx->ctx_outer_used)
4867 {
4868 ufunc->uf_flags |= FC_CLOSURE;
4869 if (outer_cctx != NULL)
4870 ++outer_cctx->ctx_closure_count;
4871 }
4872
4873 ufunc->uf_def_status = UF_COMPILED;
4874}
4875
4876/*
4877 * Perform the cleanup when a def function compilation fails.
4878 */
4879 static void
4880compile_dfunc_ufunc_cleanup(
4881 ufunc_T *ufunc,
4882 garray_T *instr,
4883 int new_def_function,
4884 char *errormsg,
4885 int did_emsg_before,
4886 cctx_T *cctx)
4887{
4888 dfunc_T *dfunc;
4889
4890 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
4891
4892 // Compiling aborted, free the generated instructions.
4893 clear_instr_ga(instr);
4894 VIM_CLEAR(dfunc->df_name);
4895 ga_clear_strings(&dfunc->df_var_names);
4896
4897 // If using the last entry in the table and it was added above, we
4898 // might as well remove it.
4899 if (!dfunc->df_deleted && new_def_function
4900 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
4901 {
4902 --def_functions.ga_len;
4903 ufunc->uf_dfunc_idx = 0;
4904 }
4905 ufunc->uf_def_status = UF_COMPILE_ERROR;
4906
4907 while (cctx->ctx_scope != NULL)
4908 drop_scope(cctx);
4909
4910 if (errormsg != NULL)
4911 emsg(errormsg);
4912 else if (did_emsg == did_emsg_before)
4913 emsg(_(e_compiling_def_function_failed));
4914}
4915
4916/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004917 * After ex_function() has collected all the function lines: parse and compile
4918 * the lines into instructions.
4919 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004920 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
4921 * the return statement (used for lambda). When uf_ret_type is already set
4922 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01004923 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004924 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02004925 * This can be used recursively through compile_lambda(), which may reallocate
4926 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02004927 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004928 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02004929 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01004930compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02004931 ufunc_T *ufunc,
4932 int check_return_type,
4933 compiletype_T compile_type,
4934 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004935{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004936 garray_T lines_to_free;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004937 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004938 cctx_T cctx;
4939 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01004940 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02004941 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004942 int ret = FAIL;
4943 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02004944 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02004945 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02004946 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02004947 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004948
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004949 // allocated lines are freed at the end
4950 ga_init2(&lines_to_free, sizeof(char_u *), 50);
4951
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004952 // Initialize the ufunc and the compilation context
4953 if (compile_dfunc_ufunc_init(ufunc, outer_cctx, compile_type,
4954 &new_def_function) == FAIL)
Bram Moolenaar96923b72022-03-15 15:57:04 +00004955 return FAIL;
Bram Moolenaar96923b72022-03-15 15:57:04 +00004956
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004957 compile_dfunc_cctx_init(&cctx, outer_cctx, ufunc, compile_type);
Bram Moolenaar985116a2020-07-12 17:31:09 +02004958
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004959 instr = &cctx.ctx_instr;
4960
Bram Moolenaar25e0f582020-05-25 22:36:50 +02004961 // Set the context to the function, it may be compiled when called from
4962 // another script. Set the script version to the most modern one.
4963 // The line number will be set in next_line_from_context().
4964 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004965 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4966
Bram Moolenaardc4c2302021-04-25 13:54:42 +02004967 // Don't use the flag from ":legacy" here.
4968 cmdmod.cmod_flags &= ~CMOD_LEGACY;
4969
Bram Moolenaar25e0f582020-05-25 22:36:50 +02004970 // Make sure error messages are OK.
4971 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
4972 if (do_estack_push)
4973 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02004974 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02004975
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004976 // Make sure arguments don't shadow variables in the context
Bram Moolenaar9a015112021-12-31 14:06:45 +00004977 if (check_args_shadowing(ufunc, &cctx) == FAIL)
4978 goto erret;
4979
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004980 // For an object method and a constructor generate instructions to
4981 // initialize "this" and the object variables.
Bram Moolenaar574950d2023-01-03 19:08:50 +00004982 if (ufunc->uf_flags & (FC_OBJECT|FC_NEW))
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004983 if (obj_method_prologue(ufunc, &cctx) == FAIL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004984 goto erret;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004985
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004986 if (ufunc->uf_def_args.ga_len > 0)
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004987 if (compile_def_function_default_args(ufunc, instr, &cctx) == FAIL)
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004988 goto erret;
Bram Moolenaare28d9b32021-07-03 18:56:53 +02004989 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004990
Ernie Rael7c92e882025-01-18 17:26:39 +01004991 // Compiling an abstract method or a function in an interface is done to
4992 // get the function type. No code is actually compiled.
4993 if (ufunc->uf_class != NULL && (IS_INTERFACE(ufunc->uf_class)
4994 || IS_ABSTRACT_METHOD(ufunc)))
Bram Moolenaarb8bebd02023-01-30 20:24:23 +00004995 {
4996 ufunc->uf_def_status = UF_NOT_COMPILED;
4997 ret = OK;
4998 goto erret;
4999 }
5000
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02005001 // compile the function body
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01005002 if (compile_def_function_body(ufunc->uf_lines.ga_len, check_return_type,
5003 &lines_to_free, &errormsg, &cctx) == FAIL)
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02005004 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005005
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01005006 if (compile_dfunc_scope_end_missing(&cctx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005007 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005008
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01005009 if (compile_dfunc_generate_default_return(ufunc, &cctx) == FAIL)
5010 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005011
Bram Moolenaar599410c2021-04-10 14:03:43 +02005012 // When compiled with ":silent!" and there was an error don't consider the
5013 // function compiled.
5014 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01005015 compile_dfunc_epilogue(outer_cctx, ufunc, instr, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005016
5017 ret = OK;
5018
5019erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02005020 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005021 {
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01005022 // compilation failed. do cleanup.
5023 compile_dfunc_ufunc_cleanup(ufunc, instr, new_def_function,
5024 errormsg, did_emsg_before, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005025 }
5026
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005027 if (cctx.ctx_redir_lhs.lhs_name != NULL)
5028 {
5029 if (ret == OK)
5030 {
5031 emsg(_(e_missing_redir_end));
5032 ret = FAIL;
5033 }
5034 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02005035 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005036 }
5037
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005038 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02005039 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02005040 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02005041 if (do_estack_push)
5042 estack_pop();
5043
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005044 ga_clear_strings(&lines_to_free);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005045 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005046 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02005047 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005048}
5049
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005050 void
5051set_function_type(ufunc_T *ufunc)
5052{
5053 int varargs = ufunc->uf_va_name != NULL;
5054 int argcount = ufunc->uf_args.ga_len;
5055
5056 // Create a type for the function, with the return type and any
5057 // argument types.
5058 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
5059 // The type is included in "tt_args".
5060 if (argcount > 0 || varargs)
5061 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01005062 if (ufunc->uf_type_list.ga_itemsize == 0)
5063 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005064 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
5065 argcount, &ufunc->uf_type_list);
5066 // Add argument types to the function type.
5067 if (func_type_add_arg_types(ufunc->uf_func_type,
5068 argcount + varargs,
5069 &ufunc->uf_type_list) == FAIL)
5070 return;
5071 ufunc->uf_func_type->tt_argcount = argcount + varargs;
5072 ufunc->uf_func_type->tt_min_argcount =
5073 argcount - ufunc->uf_def_args.ga_len;
5074 if (ufunc->uf_arg_types == NULL)
5075 {
5076 int i;
5077
5078 // lambda does not have argument types.
5079 for (i = 0; i < argcount; ++i)
5080 ufunc->uf_func_type->tt_args[i] = &t_any;
5081 }
5082 else
5083 mch_memmove(ufunc->uf_func_type->tt_args,
5084 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
5085 if (varargs)
5086 {
5087 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +02005088 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005089 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
5090 }
5091 }
5092 else
5093 // No arguments, can use a predefined type.
5094 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
5095 argcount, &ufunc->uf_type_list);
5096}
5097
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005098/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01005099 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01005100 */
5101 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005102delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01005103{
5104 int idx;
5105
Bram Moolenaard505d172022-12-18 21:42:55 +00005106 // In same cases the instructions may refer to a class in which the
5107 // function is defined and unreferencing the class may call back here
5108 // recursively. Set the df_delete_busy to avoid problems.
5109 if (dfunc->df_delete_busy)
5110 return;
5111 dfunc->df_delete_busy = TRUE;
5112
Bram Moolenaar20431c92020-03-20 18:39:46 +01005113 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005114 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005115
5116 if (dfunc->df_instr != NULL)
5117 {
5118 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
5119 delete_instr(dfunc->df_instr + idx);
5120 VIM_CLEAR(dfunc->df_instr);
5121 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +02005122 if (dfunc->df_instr_debug != NULL)
5123 {
5124 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
5125 delete_instr(dfunc->df_instr_debug + idx);
5126 VIM_CLEAR(dfunc->df_instr_debug);
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +02005127 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +01005128#ifdef FEAT_PROFILE
5129 if (dfunc->df_instr_prof != NULL)
5130 {
5131 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
5132 delete_instr(dfunc->df_instr_prof + idx);
5133 VIM_CLEAR(dfunc->df_instr_prof);
Bram Moolenaarc05fe072021-01-24 21:30:48 +01005134 }
5135#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +01005136
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005137 if (mark_deleted)
5138 dfunc->df_deleted = TRUE;
5139 if (dfunc->df_ufunc != NULL)
5140 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaard505d172022-12-18 21:42:55 +00005141
5142 dfunc->df_delete_busy = FALSE;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005143}
5144
5145/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005146 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01005147 * function, unless another user function still uses it.
5148 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005149 */
5150 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01005151unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005152{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00005153 if (ufunc->uf_dfunc_idx <= 0)
5154 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005155
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00005156 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5157 + ufunc->uf_dfunc_idx;
5158
5159 if (--dfunc->df_refcount <= 0)
5160 delete_def_function_contents(dfunc, TRUE);
5161 ufunc->uf_def_status = UF_NOT_COMPILED;
5162 ufunc->uf_dfunc_idx = 0;
5163 if (dfunc->df_ufunc == ufunc)
5164 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005165}
5166
Bram Moolenaarfdeab652020-09-19 15:16:50 +02005167/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01005168 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02005169 */
5170 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01005171link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02005172{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00005173 if (ufunc->uf_dfunc_idx <= 0)
5174 return;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02005175
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00005176 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5177 + ufunc->uf_dfunc_idx;
5178
5179 ++dfunc->df_refcount;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02005180}
5181
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005182#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01005183/*
5184 * Free all functions defined with ":def".
5185 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005186 void
5187free_def_functions(void)
5188{
Bram Moolenaar20431c92020-03-20 18:39:46 +01005189 int idx;
5190
5191 for (idx = 0; idx < def_functions.ga_len; ++idx)
5192 {
5193 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
5194
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005195 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01005196 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005197 }
5198
5199 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005200}
5201#endif
5202
5203
5204#endif // FEAT_EVAL