blob: 34a78daef4863b78c4e5d1e823c893cb826fd6c8 [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
2501 || lhs->lhs_type->tt_type == VAR_OBJECT)
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02002502 {
Yegappan Lakshmanan54d7f182025-02-10 21:35:07 +01002503 // Check whether the class or object variable is modifiable
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02002504 if (!lhs_class_member_modifiable(lhs, var_start, cctx))
2505 return FAIL;
2506 }
2507
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002508 // Now we can properly check the type. The variable is indexed, thus
2509 // we need the member type. For a class or object we don't know the
2510 // type yet, it depends on what member is used.
Yegappan Lakshmananc229a6a2023-10-26 23:05:07 +02002511 // The top item in the stack is the Dict, followed by the key and then
2512 // the type of the value.
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002513 vartype_T vartype = lhs->lhs_type->tt_type;
2514 type_T *member_type = lhs->lhs_type->tt_member;
2515 if (rhs_type != NULL && member_type != NULL
2516 && vartype != VAR_OBJECT && vartype != VAR_CLASS
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002517 && rhs_type != &t_void
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002518 && need_type(rhs_type, member_type, FALSE,
Yegappan Lakshmananc229a6a2023-10-26 23:05:07 +02002519 -3, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002520 return FAIL;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002521
2522 return OK;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002523 }
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002524
2525 return generate_loadvar(cctx, lhs);
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002526}
2527
2528/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02002529 * Produce code for loading "lhs" and also take care of an index.
2530 * Return OK/FAIL.
2531 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002532 int
Bram Moolenaara369c3d2021-04-21 16:00:10 +02002533compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
2534{
Bram Moolenaar4cae8452023-01-15 15:51:48 +00002535 if (lhs->lhs_type->tt_type == VAR_OBJECT)
2536 {
Bram Moolenaar22363c62023-04-24 17:15:25 +01002537 // "this.value": load "this" object and get the value at index for an
2538 // object or class member get the type of the member.
2539 // Also for "obj.value".
Yegappan Lakshmananff6f0d52023-12-21 16:46:18 +01002540 char_u *dot = vim_strchr(var_start, '.');
2541 if (dot == NULL)
2542 {
2543 semsg(_(e_missing_dot_after_object_str), lhs->lhs_name);
2544 return FAIL;
2545 }
Bram Moolenaar22363c62023-04-24 17:15:25 +01002546
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +02002547 class_T *cl = lhs->lhs_type->tt_class;
Yegappan Lakshmanan1ea42882023-10-11 21:43:52 +02002548 type_T *type = oc_member_type(cl, TRUE, dot + 1,
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02002549 lhs->lhs_end, &lhs->lhs_member_idx);
Bram Moolenaar4cae8452023-01-15 15:51:48 +00002550 if (lhs->lhs_member_idx < 0)
2551 return FAIL;
2552
Bram Moolenaar22363c62023-04-24 17:15:25 +01002553 if (dot - var_start == 4 && STRNCMP(var_start, "this", 4) == 0)
2554 {
2555 // load "this"
Yegappan Lakshmanand990bf02024-03-22 19:56:17 +01002556 lvar_T *lvar = lhs->lhs_lvar;
2557 int rc;
2558
2559 if (lvar->lv_from_outer > 0)
2560 rc = generate_LOADOUTER(cctx, lvar->lv_idx,
2561 lvar->lv_from_outer, lvar->lv_loop_depth,
2562 lvar->lv_loop_idx, type);
2563 else
2564 rc = generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
2565
2566 if (rc == FAIL)
Bram Moolenaar22363c62023-04-24 17:15:25 +01002567 return FAIL;
2568 }
2569 else
2570 {
2571 // load object variable or argument
2572 if (compile_load_lhs(lhs, var_start, lhs->lhs_type, cctx) == FAIL)
2573 return FAIL;
2574 }
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01002575 if (IS_INTERFACE(cl))
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02002576 return generate_GET_ITF_MEMBER(cctx, cl, lhs->lhs_member_idx, type);
2577 return generate_GET_OBJ_MEMBER(cctx, lhs->lhs_member_idx, type);
Bram Moolenaar4cae8452023-01-15 15:51:48 +00002578 }
Yegappan Lakshmanan1ea42882023-10-11 21:43:52 +02002579 else if (lhs->lhs_type->tt_type == VAR_CLASS)
2580 {
2581 // "<classname>.value": load class variable "classname.value"
Yegappan Lakshmananff6f0d52023-12-21 16:46:18 +01002582 char_u *dot = vim_strchr(var_start, '.');
2583 if (dot == NULL)
2584 {
2585 check_type_is_value(lhs->lhs_type);
2586 return FAIL;
2587 }
Yegappan Lakshmanan1ea42882023-10-11 21:43:52 +02002588
2589 class_T *cl = lhs->lhs_type->tt_class;
2590 ocmember_T *m = class_member_lookup(cl, dot + 1,
2591 lhs->lhs_end - dot - 1,
2592 &lhs->lhs_member_idx);
2593 if (m == NULL)
2594 return FAIL;
2595
2596 return generate_CLASSMEMBER(cctx, TRUE, cl, lhs->lhs_member_idx);
2597 }
Bram Moolenaar4cae8452023-01-15 15:51:48 +00002598
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002599 if (compile_load_lhs(lhs, var_start, NULL, cctx) == FAIL)
2600 return FAIL;
Bram Moolenaara369c3d2021-04-21 16:00:10 +02002601
2602 if (lhs->lhs_has_index)
2603 {
2604 int range = FALSE;
2605
2606 // Get member from list or dict. First compile the
2607 // index value.
2608 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
2609 return FAIL;
2610 if (range)
2611 {
2612 semsg(_(e_cannot_use_range_with_assignment_operator_str),
2613 var_start);
2614 return FAIL;
2615 }
2616
2617 // Get the member.
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002618 if (compile_member(FALSE, NULL, cctx) == FAIL)
Bram Moolenaara369c3d2021-04-21 16:00:10 +02002619 return FAIL;
2620 }
2621 return OK;
2622}
2623
2624/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002625 * Assignment to a list or dict member, or ":unlet" for the item, using the
2626 * information in "lhs".
2627 * Returns OK or FAIL.
2628 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002629 int
Bram Moolenaare42939a2021-04-05 17:11:17 +02002630compile_assign_unlet(
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002631 char_u *var_start,
2632 lhs_T *lhs,
2633 int is_assign,
2634 type_T *rhs_type,
2635 cctx_T *cctx)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002636{
Bram Moolenaare42939a2021-04-05 17:11:17 +02002637 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002638 int range = FALSE;
2639
Bram Moolenaar68452172021-04-12 21:21:02 +02002640 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002641 return FAIL;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002642 if (is_assign && range
2643 && lhs->lhs_type->tt_type != VAR_LIST
2644 && lhs->lhs_type != &t_blob
2645 && lhs->lhs_type != &t_any)
Bram Moolenaar68452172021-04-12 21:21:02 +02002646 {
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002647 if (lhs->lhs_type->tt_type == VAR_TUPLE)
2648 emsg(_(e_cannot_slice_tuple));
2649 else
2650 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
Bram Moolenaar68452172021-04-12 21:21:02 +02002651 return FAIL;
2652 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002653
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002654 if (lhs->lhs_type == NULL || lhs->lhs_type == &t_any)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002655 {
2656 // Index on variable of unknown type: check at runtime.
2657 dest_type = VAR_ANY;
2658 }
2659 else
2660 {
2661 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002662 if (dest_type == VAR_DICT && range)
2663 {
zeertzjq276410e2023-05-07 21:59:33 +01002664 emsg(_(e_cannot_use_range_with_dictionary));
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002665 return FAIL;
2666 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002667 if (dest_type == VAR_DICT
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02002668 && may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002669 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02002670 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002671 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002672 type_T *type;
2673
2674 if (range)
2675 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00002676 type = get_type_on_stack(cctx, 1);
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002677 if (need_type(type, &t_number, FALSE,
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002678 -2, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002679 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02002680 }
Bram Moolenaar078a4612022-01-04 15:17:03 +00002681 type = get_type_on_stack(cctx, 0);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00002682 if ((dest_type != VAR_BLOB && type->tt_type != VAR_SPECIAL)
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002683 && need_type(type, &t_number, FALSE,
Bram Moolenaar51e93322021-04-17 20:44:56 +02002684 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002685 return FAIL;
2686 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002687 }
2688
Bram Moolenaar4875d6a2022-08-17 15:55:51 +01002689 if (cctx->ctx_skip == SKIP_YES)
2690 return OK;
2691
Bram Moolenaar22363c62023-04-24 17:15:25 +01002692 // Load the dict, list or object. On the stack we then have:
Bram Moolenaar752fc692021-01-04 21:57:11 +01002693 // - value (for assignment, not for :unlet)
2694 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002695 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01002696 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002697 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
2698 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002699
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002700 if (dest_type == VAR_LIST
2701 || dest_type == VAR_DICT
2702 || dest_type == VAR_BLOB
2703 || dest_type == VAR_CLASS
2704 || dest_type == VAR_OBJECT
2705 || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002706 {
2707 if (is_assign)
2708 {
Bram Moolenaar68452172021-04-12 21:21:02 +02002709 if (range)
2710 {
2711 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
2712 return FAIL;
2713 }
2714 else
2715 {
2716 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01002717
Bram Moolenaar68452172021-04-12 21:21:02 +02002718 if (isn == NULL)
2719 return FAIL;
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002720 isn->isn_arg.storeindex.si_vartype = dest_type;
2721 isn->isn_arg.storeindex.si_class = NULL;
2722
2723 if (dest_type == VAR_OBJECT)
2724 {
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +00002725 class_T *cl = lhs->lhs_type->tt_class;
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002726
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01002727 if (IS_INTERFACE(cl))
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002728 {
2729 // "this.value": load "this" object and get the value
2730 // at index for an object or class member get the type
2731 // of the member
2732 isn->isn_arg.storeindex.si_class = cl;
2733 ++cl->class_refcount;
2734 }
2735 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002736 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002737 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002738 else if (range)
2739 {
2740 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
2741 return FAIL;
2742 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002743 else
2744 {
2745 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
2746 return FAIL;
2747 }
2748 }
2749 else
2750 {
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002751 if (dest_type == VAR_TUPLE)
2752 emsg(_(e_tuple_is_immutable));
2753 else
2754 emsg(_(e_indexable_type_required));
Bram Moolenaar752fc692021-01-04 21:57:11 +01002755 return FAIL;
2756 }
2757
2758 return OK;
2759}
2760
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002761/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002762 * Generate an instruction to push the default value for "vartype".
2763 * if "dest_local" is TRUE then for some types no instruction is generated.
2764 * "skip_store" is set to TRUE if no PUSH instruction is generated.
2765 * Returns OK or FAIL.
2766 */
2767 static int
2768push_default_value(
2769 cctx_T *cctx,
2770 vartype_T vartype,
2771 int dest_is_local,
2772 int *skip_store)
2773{
2774 int r = OK;
2775
2776 switch (vartype)
2777 {
2778 case VAR_BOOL:
2779 r = generate_PUSHBOOL(cctx, VVAL_FALSE);
2780 break;
2781 case VAR_FLOAT:
2782 r = generate_PUSHF(cctx, 0.0);
2783 break;
2784 case VAR_STRING:
2785 r = generate_PUSHS(cctx, NULL);
2786 break;
2787 case VAR_BLOB:
2788 r = generate_PUSHBLOB(cctx, blob_alloc());
2789 break;
2790 case VAR_FUNC:
2791 r = generate_PUSHFUNC(cctx, NULL, &t_func_void, TRUE);
2792 break;
2793 case VAR_LIST:
2794 r = generate_NEWLIST(cctx, 0, FALSE);
2795 break;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01002796 case VAR_TUPLE:
2797 r = generate_NEWTUPLE(cctx, 0, FALSE);
2798 break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002799 case VAR_DICT:
2800 r = generate_NEWDICT(cctx, 0, FALSE);
2801 break;
2802 case VAR_JOB:
2803 r = generate_PUSHJOB(cctx);
2804 break;
2805 case VAR_CHANNEL:
2806 r = generate_PUSHCHANNEL(cctx);
2807 break;
Ernie Rael5c018be2023-08-27 18:40:26 +02002808 case VAR_OBJECT:
2809 r = generate_PUSHOBJ(cctx);
2810 break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002811 case VAR_NUMBER:
2812 case VAR_UNKNOWN:
2813 case VAR_ANY:
2814 case VAR_PARTIAL:
2815 case VAR_VOID:
2816 case VAR_INSTR:
2817 case VAR_CLASS:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002818 case VAR_TYPEALIAS:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002819 case VAR_SPECIAL: // cannot happen
2820 // This is skipped for local variables, they are always
2821 // initialized to zero. But in a "for" or "while" loop
2822 // the value may have been changed.
2823 if (dest_is_local && !inside_loop_scope(cctx))
2824 *skip_store = TRUE;
2825 else
2826 r = generate_PUSHNR(cctx, 0);
2827 break;
2828 }
2829 return r;
2830}
2831
2832/*
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002833 * Compile assignment context. Used when compiling an assignment statement.
2834 */
2835typedef struct cac_S cac_T;
2836struct cac_S
2837{
2838 cmdidx_T cac_cmdidx; // assignment command
2839 char_u *cac_nextc; // next character to parse
2840 lhs_T cac_lhs; // lhs of the assignment
2841 type_T *cac_rhs_type; // rhs type of an assignment
2842 char_u *cac_op; // assignment operator
2843 int cac_oplen; // assignment operator length
2844 char_u *cac_var_start; // start of the variable names
2845 char_u *cac_var_end; // end of the variable names
2846 int cac_var_count; // number of variables in assignment
2847 int cac_var_idx; // variable index in a list
2848 int cac_semicolon; // semicolon in [var1, var2; var3]
2849 garray_T *cac_instr;
2850 int cac_instr_count;
2851 int cac_incdec;
2852 int cac_did_generate_slice;
2853 int cac_is_decl;
2854 int cac_is_const;
2855 int cac_start_lnum;
2856 type_T *cac_inferred_type;
2857 int cac_skip_store;
2858};
2859
2860/*
2861 * Initialize the compile assignment context.
2862 */
2863 static void
2864compile_assign_context_init(cac_T *cac, cctx_T *cctx, int cmdidx, char_u *arg)
2865{
2866 CLEAR_FIELD(*cac);
2867 cac->cac_cmdidx = cmdidx;
2868 cac->cac_instr = &cctx->ctx_instr;
2869 cac->cac_rhs_type = &t_any;
2870 cac->cac_is_decl = is_decl_command(cmdidx);
2871 cac->cac_start_lnum = SOURCING_LNUM;
2872 cac->cac_instr_count = -1;
2873 cac->cac_var_end = arg;
2874}
2875
2876/*
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01002877 * Compile an object member variable assignment in the arguments passed to a
2878 * class new() method.
2879 *
2880 * Instruction format:
2881 *
2882 * ifargisset <n> this.<varname> = <value>
2883 *
2884 * where <n> is the index of the default argument.
2885 *
2886 * Generates the ISN_JUMP_IF_ARG_NOT_SET instruction to skip the assignment if
2887 * the value is passed as an argument to the new() method call.
2888 *
2889 * Returns OK on success.
2890 */
2891 static int
2892compile_assign_obj_new_arg(char_u **argp, cctx_T *cctx)
2893{
2894 char_u *arg = *argp;
2895
2896 arg += 11; // skip "ifargisset"
2897 int def_arg_idx = getdigits(&arg);
2898 arg = skipwhite(arg);
2899
2900 // Use a JUMP_IF_ARG_NOT_SET instruction to skip if the value was not
2901 // given and the default value is "v:none".
2902 int stack_offset = STACK_FRAME_SIZE +
2903 (cctx->ctx_ufunc->uf_va_name != NULL ? 1 : 0);
2904 int def_arg_count = cctx->ctx_ufunc->uf_def_args.ga_len;
2905 int arg_offset = def_arg_idx - def_arg_count - stack_offset;
2906
2907 if (generate_JUMP_IF_ARG(cctx, ISN_JUMP_IF_ARG_NOT_SET,
2908 arg_offset) == FAIL)
2909 return FAIL;
2910
2911 *argp = arg;
2912 return OK;
2913}
2914
2915/*
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002916 * Translate the increment (++) and decrement (--) operators to the
2917 * corresponding compound operators (+= or -=).
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01002918 *
2919 * Returns OK on success and FAIL on syntax error.
2920 */
2921 static int
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002922translate_incdec_op(exarg_T *eap, cac_T *cac)
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01002923{
2924 if (VIM_ISWHITE(eap->cmd[2]))
2925 {
2926 semsg(_(e_no_white_space_allowed_after_str_str),
2927 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
2928 return FAIL;
2929 }
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002930 cac->cac_op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
2931 cac->cac_oplen = 2;
2932 cac->cac_incdec = TRUE;
2933
2934 return OK;
2935}
2936
2937/*
2938 * Process the operator in an assignment statement.
2939 */
2940 static int
2941compile_assign_process_operator(
2942 exarg_T *eap,
2943 char_u *arg,
2944 cac_T *cac,
2945 int *heredoc,
2946 char_u **retstr)
2947{
2948 *retstr = NULL;
2949
2950 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002951 // Change an unary operator to a compound operator
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01002952 return translate_incdec_op(eap, cac);
2953
2954 char_u *sp = cac->cac_nextc;
2955 cac->cac_nextc = skipwhite(cac->cac_nextc);
2956 cac->cac_op = cac->cac_nextc;
2957 cac->cac_oplen = assignment_len(cac->cac_nextc, heredoc);
2958
2959 if (cac->cac_var_count > 0 && cac->cac_oplen == 0)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002960 {
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01002961 // can be something like "[1, 2]->func()"
2962 *retstr = arg;
2963 return FAIL;
2964 }
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002965
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01002966 // need white space before and after the operator
2967 if (cac->cac_oplen > 0 && (!VIM_ISWHITE(*sp)
2968 || !IS_WHITE_OR_NUL(cac->cac_op[cac->cac_oplen])))
2969 {
2970 error_white_both(cac->cac_op, cac->cac_oplen);
2971 return FAIL;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002972 }
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01002973
2974 return OK;
2975}
2976
2977/*
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01002978 * Find the start of an assignment statement.
2979 */
2980 static char_u *
2981compile_assign_compute_start(char_u *arg, int var_count)
2982{
2983 if (var_count > 0)
2984 // [var1, var2] = [val1, val2]
2985 // skip over the "["
2986 return skipwhite(arg + 1);
2987
2988 return arg;
2989}
2990
2991/*
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01002992 * Parse a heredoc assignment starting at "p". Returns a pointer to the
2993 * beginning of the heredoc content.
2994 */
2995 static char_u *
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002996parse_heredoc_assignment(exarg_T *eap, cctx_T *cctx, cac_T *cac)
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01002997{
2998 // [let] varname =<< [trim] {end}
2999 eap->ea_getline = exarg_getline;
3000 eap->cookie = cctx;
3001
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003002 list_T *l = heredoc_get(eap, cac->cac_nextc + 3, FALSE, TRUE);
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003003 if (l == NULL)
3004 return NULL;
3005
3006 list_free(l);
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003007 cac->cac_nextc += STRLEN(cac->cac_nextc);
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003008
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003009 return cac->cac_nextc;
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003010}
3011
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003012/*
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003013 * Check the type of a RHS expression in a list assignment statement.
3014 * The RHS expression is already compiled. So the type is on the stack.
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003015 */
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003016 static int
3017compile_assign_list_check_rhs_type(cctx_T *cctx, cac_T *cac)
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003018{
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003019 type_T *stacktype;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003020
3021 stacktype = cctx->ctx_type_stack.ga_len == 0 ? &t_void
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003022 : get_type_on_stack(cctx, 0);
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003023 if (stacktype->tt_type == VAR_VOID)
3024 {
3025 emsg(_(e_cannot_use_void_value));
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003026 return FAIL;
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003027 }
3028
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01003029 if (stacktype->tt_type != VAR_LIST && stacktype->tt_type != VAR_TUPLE
3030 && stacktype->tt_type != VAR_ANY)
3031 {
3032 emsg(_(e_list_or_tuple_required));
3033 return FAIL;
3034 }
3035
3036 if (need_type(stacktype,
3037 stacktype->tt_type == VAR_TUPLE ? &t_tuple_any : &t_list_any,
3038 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003039 return FAIL;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003040
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01003041 if (stacktype->tt_type == VAR_TUPLE)
3042 {
3043 if (stacktype->tt_argcount != 1)
3044 cac->cac_rhs_type = &t_any;
3045 else
3046 {
3047 if (stacktype->tt_flags & TTFLAG_VARARGS)
3048 cac->cac_rhs_type = stacktype->tt_args[0]->tt_member;
3049 else
3050 cac->cac_rhs_type = stacktype->tt_args[0];
3051 }
3052 }
3053 else if (stacktype->tt_member != NULL)
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003054 cac->cac_rhs_type = stacktype->tt_member;
3055
3056 return OK;
3057}
3058
3059/*
3060 * In a list assignment statement, if a constant list was used, check the
3061 * length. Returns OK if the length check succeeds. Returns FAIL otherwise.
3062 */
3063 static int
3064compile_assign_list_check_length(cctx_T *cctx, cac_T *cac)
3065{
3066 int needed_list_len;
3067 int did_check = FALSE;
3068
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003069 needed_list_len = cac->cac_semicolon
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003070 ? cac->cac_var_count - 1
3071 : cac->cac_var_count;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003072 if (cac->cac_instr->ga_len > 0)
3073 {
3074 isn_T *isn = ((isn_T *)cac->cac_instr->ga_data) +
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003075 cac->cac_instr->ga_len - 1;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003076
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01003077 if (isn->isn_type == ISN_NEWLIST || isn->isn_type == ISN_NEWTUPLE)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003078 {
3079 did_check = TRUE;
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003080 if (cac->cac_semicolon ?
3081 isn->isn_arg.number < needed_list_len
3082 : isn->isn_arg.number != needed_list_len)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003083 {
3084 semsg(_(e_expected_nr_items_but_got_nr),
3085 needed_list_len, (int)isn->isn_arg.number);
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003086 return FAIL;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003087 }
3088 }
3089 }
3090
3091 if (!did_check)
3092 generate_CHECKLEN(cctx, needed_list_len, cac->cac_semicolon);
3093
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003094 return OK;
3095}
3096
3097/*
3098 * Evaluate the expression for "[var, var] = expr" assignment.
3099 * A line break may follow the assignment operator "=".
3100 */
3101 static char_u *
3102compile_assign_list_expr(cctx_T *cctx, cac_T *cac)
3103{
3104 char_u *whitep;
3105
3106 whitep = cac->cac_op + cac->cac_oplen;
3107
3108 if (may_get_next_line_error(whitep, &cac->cac_nextc, cctx) == FAIL)
3109 return NULL;
3110
3111 // compile RHS expression
3112 if (compile_expr0(&cac->cac_nextc, cctx) == FAIL)
3113 return NULL;
3114
3115 if (cctx->ctx_skip == SKIP_YES)
3116 // no need to parse more when skipping
3117 return cac->cac_nextc;
3118
3119 if (compile_assign_list_check_rhs_type(cctx, cac) == FAIL)
3120 return NULL;
3121
3122 // If a constant list was used we can check the length right here.
3123 if (compile_assign_list_check_length(cctx, cac) == FAIL)
3124 return FAIL;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003125
3126 return cac->cac_nextc;
3127}
3128
3129/*
3130 * Find and return the end of a heredoc or a list of variables assignment
3131 * statement. For a single variable assignment statement, returns the current
3132 * end.
3133 * Returns NULL on failure.
3134 */
3135 static char_u *
3136compile_assign_compute_end(
3137 exarg_T *eap,
3138 cctx_T *cctx,
3139 cac_T *cac,
3140 int heredoc)
3141{
3142 if (heredoc)
3143 {
3144 cac->cac_nextc = parse_heredoc_assignment(eap, cctx, cac);
3145 return cac->cac_nextc;
3146 }
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003147
3148 if (cac->cac_var_count > 0)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003149 {
3150 // for "[var, var] = expr" evaluate the expression. The list of
3151 // variables are processed later.
3152 // A line break may follow the "=".
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003153 cac->cac_nextc = compile_assign_list_expr(cctx, cac);
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003154 return cac->cac_nextc;
3155 }
3156
3157 return cac->cac_var_end;
3158}
3159
3160/*
3161 * For "var = expr" evaluate the expression.
3162 */
3163 static int
3164compile_assign_single_eval_expr(cctx_T *cctx, cac_T *cac)
3165{
3166 int ret = OK;
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003167 char_u *whitep;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003168 lhs_T *lhs = &cac->cac_lhs;
3169
3170 // Compile the expression.
3171 if (cac->cac_incdec)
3172 return generate_PUSHNR(cctx, 1);
3173
3174 // Temporarily hide the new local variable here, it is
3175 // not available to this expression.
3176 if (lhs->lhs_new_local)
3177 --cctx->ctx_locals.ga_len;
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003178 whitep = cac->cac_op + cac->cac_oplen;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003179
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003180 if (may_get_next_line_error(whitep, &cac->cac_nextc, cctx) == FAIL)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003181 {
3182 if (lhs->lhs_new_local)
3183 ++cctx->ctx_locals.ga_len;
3184 return FAIL;
3185 }
3186
3187 ret = compile_expr0_ext(&cac->cac_nextc, cctx, &cac->cac_is_const);
3188 if (lhs->lhs_new_local)
3189 ++cctx->ctx_locals.ga_len;
3190
3191 return ret;
3192}
3193
3194/*
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003195 * When compiling an assignment, set the LHS type to the RHS type.
3196 */
3197 static int
3198compile_assign_set_lhs_type_from_rhs(
3199 cctx_T *cctx,
3200 cac_T *cac,
3201 lhs_T *lhs,
3202 type_T *rhs_type)
3203{
3204 if (rhs_type->tt_type == VAR_VOID)
3205 {
3206 emsg(_(e_cannot_use_void_value));
3207 return FAIL;
3208 }
3209
3210 type_T *type;
3211
3212 // An empty list or dict has a &t_unknown member, for a variable that
3213 // implies &t_any.
3214 if (rhs_type == &t_list_empty)
3215 type = &t_list_any;
3216 else if (rhs_type == &t_dict_empty)
3217 type = &t_dict_any;
3218 else if (rhs_type == &t_unknown)
3219 type = &t_any;
3220 else
3221 {
3222 type = rhs_type;
3223 cac->cac_inferred_type = rhs_type;
3224 }
3225
3226 set_var_type(lhs->lhs_lvar, type, cctx);
3227
3228 return OK;
3229}
3230
3231/*
3232 * Returns TRUE if the "rhs_type" can be assigned to the "lhs" variable.
3233 * Used when compiling an assignment statement.
3234 */
3235 static int
3236compile_assign_valid_rhs_type(
3237 cctx_T *cctx,
3238 cac_T *cac,
3239 lhs_T *lhs,
3240 type_T *rhs_type)
3241{
3242 type_T *use_type = lhs->lhs_lvar->lv_type;
3243 where_T where = WHERE_INIT;
3244
3245 // Without operator check type here, otherwise below.
3246 // Use the line number of the assignment.
3247 SOURCING_LNUM = cac->cac_start_lnum;
3248 if (cac->cac_var_count > 0)
3249 {
3250 where.wt_index = cac->cac_var_idx + 1;
3251 where.wt_kind = WT_VARIABLE;
3252 }
3253
3254 // If assigning to a list or dict member, use the member type.
3255 // Not for "list[:] =".
3256 if (lhs->lhs_has_index &&
3257 !has_list_index(cac->cac_var_start + lhs->lhs_varlen, cctx))
3258 use_type = lhs->lhs_member_type;
3259
3260 if (need_type_where(rhs_type, use_type, FALSE, -1, where, cctx, FALSE,
3261 cac->cac_is_const) == FAIL)
3262 return FALSE;
3263
3264 return TRUE;
3265}
3266
3267/*
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003268 * Compare the LHS type with the RHS type in an assignment.
3269 */
3270 static int
3271compile_assign_check_type(cctx_T *cctx, cac_T *cac)
3272{
3273 lhs_T *lhs = &cac->cac_lhs;
3274 type_T *rhs_type;
3275
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003276 rhs_type = cctx->ctx_type_stack.ga_len == 0
3277 ? &t_void
3278 : get_type_on_stack(cctx, 0);
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003279 cac->cac_rhs_type = rhs_type;
3280
3281 if (check_type_is_value(rhs_type) == FAIL)
3282 return FAIL;
3283
3284 if (lhs->lhs_lvar != NULL && (cac->cac_is_decl || !lhs->lhs_has_type))
3285 {
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003286 if (rhs_type->tt_type == VAR_FUNC
3287 || rhs_type->tt_type == VAR_PARTIAL)
3288 {
3289 // Make sure the variable name can be used as a funcref
3290 if (!lhs->lhs_has_index
3291 && var_wrong_func_name(lhs->lhs_name, TRUE))
3292 return FAIL;
3293 }
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003294
3295 if (lhs->lhs_new_local && !lhs->lhs_has_type)
3296 {
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003297 // The LHS variable doesn't have a type. Set it to the RHS type.
3298 if (compile_assign_set_lhs_type_from_rhs(cctx, cac, lhs, rhs_type)
3299 == FAIL)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003300 return FAIL;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003301 }
3302 else if (*cac->cac_op == '=')
3303 {
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003304 if (!compile_assign_valid_rhs_type(cctx, cac, lhs, rhs_type))
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003305 return FAIL;
3306 }
3307 }
3308 else
3309 {
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003310 // Assigning to a register using @r = "abc"
3311
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003312 type_T *lhs_type = lhs->lhs_member_type;
3313
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003314 // Special case: assigning to @# can use a number or a string.
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003315 // Also: can assign a number to a float.
3316 if ((lhs_type == &t_number_or_string || lhs_type == &t_float)
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003317 && rhs_type->tt_type == VAR_NUMBER)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003318 lhs_type = &t_number;
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003319
3320 if (*cac->cac_nextc != '=')
3321 {
3322 if (need_type(rhs_type, lhs_type, FALSE, -1, 0, cctx, FALSE,
3323 FALSE) == FAIL)
3324 return FAIL;
3325 }
3326 }
3327
3328 return OK;
3329}
3330
3331/*
3332 * Compile the RHS expression in an assignment statement and generate the
3333 * instructions.
3334 */
3335 static int
3336compile_assign_rhs_expr(cctx_T *cctx, cac_T *cac)
3337{
3338 cac->cac_is_const = FALSE;
3339
3340 // for "+=", "*=", "..=" etc. first load the current value
3341 if (*cac->cac_op != '='
3342 && compile_load_lhs_with_index(&cac->cac_lhs, cac->cac_var_start,
3343 cctx) == FAIL)
3344 return FAIL;
3345
3346 // For "var = expr" evaluate the expression.
3347 if (cac->cac_var_count == 0)
3348 {
3349 int ret;
3350
3351 // Compile the expression.
3352 cac->cac_instr_count = cac->cac_instr->ga_len;
3353 ret = compile_assign_single_eval_expr(cctx, cac);
3354 if (ret == FAIL)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003355 return FAIL;
3356 }
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003357 else if (cac->cac_semicolon && cac->cac_var_idx == cac->cac_var_count - 1)
3358 {
3359 // For "[var; var] = expr" get the rest of the list
3360 cac->cac_did_generate_slice = TRUE;
3361 if (generate_SLICE(cctx, cac->cac_var_count - 1) == FAIL)
3362 return FAIL;
3363 }
3364 else
3365 {
3366 // For "[var, var] = expr" get the "var_idx" item from the
3367 // list.
3368 int with_op = *cac->cac_op != '=';
3369 if (generate_GETITEM(cctx, cac->cac_var_idx, with_op) == FAIL)
3370 return FAIL;
3371 }
3372
3373 if (compile_assign_check_type(cctx, cac) == FAIL)
3374 return FAIL;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003375
3376 return OK;
3377}
3378
3379/*
3380 * Compile the RHS expression in an assignment
3381 */
3382 static int
3383compile_assign_rhs(cctx_T *cctx, cac_T *cac)
3384{
3385 lhs_T *lhs = &cac->cac_lhs;
3386
3387 if (cctx->ctx_skip == SKIP_YES)
3388 {
3389 if (cac->cac_oplen > 0 && cac->cac_var_count == 0)
3390 {
3391 // skip over the "=" and the expression
3392 cac->cac_nextc = skipwhite(cac->cac_op + cac->cac_oplen);
3393 (void)compile_expr0(&cac->cac_nextc, cctx);
3394 }
3395 return OK;
3396 }
3397
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003398 // If RHS is specified, then generate instructions for RHS expression
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003399 if (cac->cac_oplen > 0)
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003400 return compile_assign_rhs_expr(cctx, cac);
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003401
3402 if (cac->cac_cmdidx == CMD_final)
3403 {
3404 emsg(_(e_final_requires_a_value));
3405 return FAIL;
3406 }
3407
3408 if (cac->cac_cmdidx == CMD_const)
3409 {
3410 emsg(_(e_const_requires_a_value));
3411 return FAIL;
3412 }
3413
3414 if (!lhs->lhs_has_type || lhs->lhs_dest == dest_option
3415 || lhs->lhs_dest == dest_func_option)
3416 {
3417 emsg(_(e_type_or_initialization_required));
3418 return FAIL;
3419 }
3420
3421 // variables are always initialized
3422 if (GA_GROW_FAILS(cac->cac_instr, 1))
3423 return FAIL;
3424
3425 cac->cac_instr_count = cac->cac_instr->ga_len;
3426
3427 return push_default_value(cctx, lhs->lhs_member_type->tt_type,
3428 lhs->lhs_dest == dest_local,
3429 &cac->cac_skip_store);
3430}
3431
3432/*
3433 * Compile a compound op assignment statement (+=, -=, *=, %=, etc.)
3434 */
3435 static int
3436compile_assign_compound_op(cctx_T *cctx, cac_T *cac)
3437{
3438 lhs_T *lhs = &cac->cac_lhs;
3439 type_T *expected;
3440 type_T *stacktype = NULL;
3441
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01003442 if (cac->cac_lhs.lhs_type->tt_type == VAR_TUPLE)
3443 {
3444 // compound operators are not supported with a tuple
3445 char_u op[2];
3446
3447 op[0] = *cac->cac_op;
3448 op[1] = NUL;
3449 semsg(_(e_wrong_variable_type_for_str_equal), op);
3450 return FAIL;
3451 }
3452
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003453 if (*cac->cac_op == '.')
3454 {
3455 if (may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL)
3456 return FAIL;
3457 }
3458 else
3459 {
3460 expected = lhs->lhs_member_type;
3461 stacktype = get_type_on_stack(cctx, 0);
3462 if (
3463 // If variable is float operation with number is OK.
3464 !(expected == &t_float && (stacktype == &t_number
3465 || stacktype == &t_number_bool))
3466 && need_type(stacktype, expected, TRUE, -1, 0, cctx,
3467 FALSE, FALSE) == FAIL)
3468 return FAIL;
3469 }
3470
3471 if (*cac->cac_op == '.')
3472 {
3473 if (generate_CONCAT(cctx, 2) == FAIL)
3474 return FAIL;
3475 }
3476 else if (*cac->cac_op == '+')
3477 {
3478 if (generate_add_instr(cctx,
3479 operator_type(lhs->lhs_member_type, stacktype),
3480 lhs->lhs_member_type, stacktype,
3481 EXPR_APPEND) == FAIL)
3482 return FAIL;
3483 }
3484 else if (generate_two_op(cctx, cac->cac_op) == FAIL)
3485 return FAIL;
3486
3487 return OK;
3488}
3489
3490/*
3491 * Generate the STORE and SETTYPE instructions for an assignment statement.
3492 */
3493 static int
3494compile_assign_generate_store(cctx_T *cctx, cac_T *cac)
3495{
3496 lhs_T *lhs = &cac->cac_lhs;
3497 int save_lnum;
3498
3499 // Use the line number of the assignment for store instruction.
3500 save_lnum = cctx->ctx_lnum;
3501 cctx->ctx_lnum = cac->cac_start_lnum - 1;
3502
3503 if (lhs->lhs_has_index)
3504 {
3505 // Use the info in "lhs" to store the value at the index in the
3506 // list, dict or object.
3507 if (compile_assign_unlet(cac->cac_var_start, &cac->cac_lhs,
3508 TRUE, cac->cac_rhs_type, cctx) == FAIL)
3509 {
3510 cctx->ctx_lnum = save_lnum;
3511 return FAIL;
3512 }
3513 }
3514 else
3515 {
3516 if (cac->cac_is_decl && cac->cac_cmdidx == CMD_const &&
3517 (lhs->lhs_dest == dest_script
3518 || lhs->lhs_dest == dest_script_v9
3519 || lhs->lhs_dest == dest_global
3520 || lhs->lhs_dest == dest_local))
3521 // ":const var": lock the value, but not referenced variables
3522 generate_LOCKCONST(cctx);
3523
3524 type_T *inferred_type = cac->cac_inferred_type;
3525
3526 if ((lhs->lhs_type->tt_type == VAR_DICT
3527 || lhs->lhs_type->tt_type == VAR_LIST)
3528 && lhs->lhs_type->tt_member != NULL
3529 && lhs->lhs_type->tt_member != &t_any
3530 && lhs->lhs_type->tt_member != &t_unknown)
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01003531 // Set the type in the list or dict, so that it can be
3532 // checked, also in legacy script.
3533 generate_SETTYPE(cctx, lhs->lhs_type);
3534 else if (lhs->lhs_type->tt_type == VAR_TUPLE
3535 && lhs->lhs_type->tt_argcount != 0)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003536 generate_SETTYPE(cctx, lhs->lhs_type);
3537 else if (inferred_type != NULL
3538 && (inferred_type->tt_type == VAR_DICT
3539 || inferred_type->tt_type == VAR_LIST)
3540 && inferred_type->tt_member != NULL
3541 && inferred_type->tt_member != &t_unknown
3542 && inferred_type->tt_member != &t_any)
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01003543 // Set the type in the list or dict, so that it can be
3544 // checked, also in legacy script.
3545 generate_SETTYPE(cctx, inferred_type);
3546 else if (inferred_type != NULL
3547 && inferred_type->tt_type == VAR_TUPLE
3548 && inferred_type->tt_argcount > 0)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003549 generate_SETTYPE(cctx, inferred_type);
3550
3551 if (!cac->cac_skip_store &&
3552 generate_store_lhs(cctx, &cac->cac_lhs,
3553 cac->cac_instr_count,
3554 cac->cac_is_decl) == FAIL)
3555 {
3556 cctx->ctx_lnum = save_lnum;
3557 return FAIL;
3558 }
3559 }
3560
3561 cctx->ctx_lnum = save_lnum;
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003562
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003563 return OK;
3564}
3565
3566/*
3567 * Process the variable(s) in an assignment statement
3568 */
3569 static int
3570compile_assign_process_variables(
3571 cctx_T *cctx,
3572 cac_T *cac,
3573 int cmdidx,
3574 int heredoc,
3575 int has_cmd,
3576 int has_argisset_prefix,
3577 int jump_instr_idx)
3578{
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003579 /*
3580 * Loop over variables in "[var, var] = expr".
3581 * For "name = expr" and "var name: type" this is done only once.
3582 */
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003583 for (cac->cac_var_idx = 0; cac->cac_var_idx == 0 ||
3584 cac->cac_var_idx < cac->cac_var_count; cac->cac_var_idx++)
3585 {
3586 if (cac->cac_var_start[0] == '_'
3587 && !eval_isnamec(cac->cac_var_start[1]))
3588 {
3589 // Ignore underscore in "[a, _, b] = list".
3590 if (cac->cac_var_count > 0)
3591 {
3592 cac->cac_var_start = skipwhite(cac->cac_var_start + 2);
3593 continue;
3594 }
3595 emsg(_(e_cannot_use_underscore_here));
3596 return FAIL;
3597 }
3598 vim_free(cac->cac_lhs.lhs_name);
3599
3600 /*
3601 * Figure out the LHS type and other properties.
3602 */
3603 if (compile_assign_lhs(cac->cac_var_start, &cac->cac_lhs, cmdidx,
3604 cac->cac_is_decl, heredoc, has_cmd,
3605 cac->cac_oplen, cctx) == FAIL)
3606 return FAIL;
3607
3608 // Compile the RHS expression
3609 if (heredoc)
3610 {
3611 SOURCING_LNUM = cac->cac_start_lnum;
3612 if (cac->cac_lhs.lhs_has_type
3613 && need_type(&t_list_string, cac->cac_lhs.lhs_type,
3614 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
3615 return FAIL;
3616 }
3617 else
3618 {
3619 if (compile_assign_rhs(cctx, cac) == FAIL)
3620 return FAIL;
3621 if (cac->cac_var_count == 0)
3622 cac->cac_var_end = cac->cac_nextc;
3623 }
3624
3625 // no need to parse more when skipping
3626 if (cctx->ctx_skip == SKIP_YES)
3627 break;
3628
3629 if (cac->cac_oplen > 0 && *cac->cac_op != '=')
3630 {
3631 if (compile_assign_compound_op(cctx, cac) == FAIL)
3632 return FAIL;
3633 }
3634
3635 // generate the store instructions
3636 if (compile_assign_generate_store(cctx, cac) == FAIL)
3637 return FAIL;
3638
3639 if (cac->cac_var_idx + 1 < cac->cac_var_count)
3640 cac->cac_var_start = skipwhite(cac->cac_lhs.lhs_end + 1);
3641
3642 if (has_argisset_prefix)
3643 {
3644 // set instruction index in JUMP_IF_ARG_SET to here
3645 isn_T *isn = ((isn_T *)cac->cac_instr->ga_data) + jump_instr_idx;
3646 isn->isn_arg.jumparg.jump_where = cac->cac_instr->ga_len;
3647 }
3648 }
3649
3650 return OK;
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003651}
3652
3653/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003654 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003655 * "let name"
3656 * "var name = expr"
3657 * "final name = expr"
3658 * "const name = expr"
3659 * "name = expr"
3660 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003661 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003662 * Return NULL for an error.
3663 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003664 */
3665 static char_u *
Bram Moolenaar65b0d162022-12-13 18:43:22 +00003666compile_assignment(
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003667 char_u *arg_start,
3668 exarg_T *eap,
3669 cmdidx_T cmdidx,
3670 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003671{
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003672 cac_T cac;
Bram Moolenaar65b0d162022-12-13 18:43:22 +00003673 char_u *arg = arg_start;
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003674 char_u *retstr = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675 int heredoc = FALSE;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003676 int jump_instr_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003677
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003678 compile_assign_context_init(&cac, cctx, cmdidx, arg);
3679
3680 jump_instr_idx = cac.cac_instr->ga_len;
3681
3682 // process object variable initialization in a new() constructor method
3683 int has_argisset_prefix = STRNCMP(arg, "ifargisset ", 11) == 0;
3684 if (has_argisset_prefix &&
3685 compile_assign_obj_new_arg(&arg, cctx) == FAIL)
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003686 goto theend;
Bram Moolenaar65b0d162022-12-13 18:43:22 +00003687
Bram Moolenaare1d12112022-03-05 11:37:48 +00003688 // Skip over the "varname" or "[varname, varname]" to get to any "=".
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003689 cac.cac_nextc = skip_var_list(arg, TRUE, &cac.cac_var_count,
3690 &cac.cac_semicolon, TRUE);
3691 if (cac.cac_nextc == NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003692 return *arg == '[' ? arg : NULL;
3693
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003694 if (compile_assign_process_operator(eap, arg, &cac, &heredoc,
3695 &retstr) == FAIL)
3696 return retstr;
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003697
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003698 // Compute the start of the assignment
3699 cac.cac_var_start = compile_assign_compute_start(arg, cac.cac_var_count);
3700
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003701 // Compute the end of the assignment
3702 cac.cac_var_end = compile_assign_compute_end(eap, cctx, &cac, heredoc);
3703 if (cac.cac_var_end == NULL)
3704 return NULL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003705
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003706 int has_cmd = cac.cac_var_start > eap->cmd;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003707
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003708 /* process the variable(s) */
3709 if (compile_assign_process_variables(cctx, &cac, cmdidx, heredoc,
3710 has_cmd, has_argisset_prefix,
3711 jump_instr_idx) == FAIL)
3712 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003713
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02003714 // For "[var, var] = expr" drop the "expr" value.
3715 // Also for "[var, var; _] = expr".
Yegappan Lakshmanane2038412024-12-14 19:59:24 +01003716 if (cctx->ctx_skip != SKIP_YES && cac.cac_var_count > 0 &&
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003717 (!cac.cac_semicolon || !cac.cac_did_generate_slice))
Bram Moolenaar9af78762020-06-16 11:34:42 +02003718 {
Bram Moolenaarec792292020-12-13 21:26:56 +01003719 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02003720 goto theend;
3721 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003722
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003723 retstr = skipwhite(cac.cac_var_end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003724
3725theend:
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003726 vim_free(cac.cac_lhs.lhs_name);
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003727 return retstr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003728}
3729
3730/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01003731 * Check for an assignment at "eap->cmd", compile it if found.
3732 * Return NOTDONE if there is none, FAIL for failure, OK if done.
3733 */
3734 static int
3735may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
3736{
3737 char_u *pskip;
3738 char_u *p;
3739
3740 // Assuming the command starts with a variable or function name,
3741 // find what follows.
3742 // Skip over "var.member", "var[idx]" and the like.
3743 // Also "&opt = val", "$ENV = val" and "@r = val".
3744 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
3745 ? eap->cmd + 1 : eap->cmd;
3746 p = to_name_end(pskip, TRUE);
3747 if (p > eap->cmd && *p != NUL)
3748 {
3749 char_u *var_end;
3750 int oplen;
3751 int heredoc;
3752
3753 if (eap->cmd[0] == '@')
3754 var_end = eap->cmd + 2;
3755 else
3756 var_end = find_name_end(pskip, NULL, NULL,
3757 FNE_CHECK_START | FNE_INCL_BR);
3758 oplen = assignment_len(skipwhite(var_end), &heredoc);
3759 if (oplen > 0)
3760 {
3761 size_t len = p - eap->cmd;
3762
3763 // Recognize an assignment if we recognize the variable
3764 // name:
Bram Moolenaar17126b12021-01-07 22:03:02 +01003765 // "&opt = expr"
3766 // "$ENV = expr"
3767 // "@r = expr"
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003768 // "g:var = expr"
3769 // "g:[key] = expr"
3770 // "local = expr" where "local" is a local var.
3771 // "script = expr" where "script" is a script-local var.
3772 // "import = expr" where "import" is an imported var
Bram Moolenaar17126b12021-01-07 22:03:02 +01003773 if (*eap->cmd == '&'
3774 || *eap->cmd == '$'
3775 || *eap->cmd == '@'
3776 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003777 || STRNCMP(eap->cmd, "g:[", 3) == 0
Bram Moolenaare0890d62021-02-17 14:52:14 +01003778 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01003779 {
3780 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
3781 if (*line == NULL || *line == eap->cmd)
3782 return FAIL;
3783 return OK;
3784 }
3785 }
3786 }
3787
Bram Moolenaar65b0d162022-12-13 18:43:22 +00003788 // might be "[var, var] = expr" or "ifargisset this.member = expr"
3789 if (*eap->cmd == '[' || STRNCMP(eap->cmd, "ifargisset ", 11) == 0)
Bram Moolenaar17126b12021-01-07 22:03:02 +01003790 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01003791 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
3792 if (*line == NULL)
3793 return FAIL;
3794 if (*line != eap->cmd)
3795 return OK;
3796 }
3797 return NOTDONE;
3798}
3799
Bram Moolenaar9a015112021-12-31 14:06:45 +00003800/*
3801 * Check if arguments of "ufunc" shadow variables in "cctx".
3802 * Return OK or FAIL.
3803 */
3804 static int
3805check_args_shadowing(ufunc_T *ufunc, cctx_T *cctx)
3806{
3807 int i;
3808 char_u *arg;
3809 int r = OK;
3810
3811 // Make sure arguments are not found when compiling a second time.
3812 ufunc->uf_args_visible = 0;
3813
3814 // Check for arguments shadowing variables from the context.
3815 for (i = 0; i < ufunc->uf_args.ga_len; ++i)
3816 {
3817 arg = ((char_u **)(ufunc->uf_args.ga_data))[i];
Bram Moolenaardce24412022-02-08 20:35:30 +00003818 if (check_defined(arg, STRLEN(arg), cctx, NULL, TRUE) == FAIL)
Bram Moolenaar9a015112021-12-31 14:06:45 +00003819 {
3820 r = FAIL;
3821 break;
3822 }
3823 }
3824 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
3825 return r;
3826}
3827
Bram Moolenaarfcb86b02022-10-07 22:46:24 +01003828#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar139575d2022-03-15 19:29:30 +00003829/*
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003830 * Get a count before a command. Can only be a number.
3831 * Returns zero if there is no count.
3832 * Returns -1 if there is something wrong.
3833 */
3834 static long
3835get_cmd_count(char_u *line, exarg_T *eap)
3836{
3837 char_u *p;
3838
3839 // skip over colons and white space
3840 for (p = line; *p == ':' || VIM_ISWHITE(*p); ++p)
3841 ;
Keith Thompson184f71c2024-01-04 21:19:04 +01003842 if (!SAFE_isdigit(*p))
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003843 {
Bram Moolenaar2435adf2022-10-21 12:05:46 +01003844 // The command or modifiers must be following. Assume a lower case
3845 // character means there is a modifier.
3846 if (p < eap->cmd && !vim_islower(*p))
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003847 {
3848 emsg(_(e_invalid_range));
3849 return -1;
3850 }
3851 return 0;
3852 }
3853 return atol((char *)p);
3854}
Bram Moolenaarfcb86b02022-10-07 22:46:24 +01003855#endif
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003856
3857/*
Bram Moolenaar139575d2022-03-15 19:29:30 +00003858 * Get the compilation type that should be used for "ufunc".
3859 * Keep in sync with INSTRUCTIONS().
3860 */
3861 compiletype_T
3862get_compile_type(ufunc_T *ufunc)
3863{
3864 // Update uf_has_breakpoint if needed.
3865 update_has_breakpoint(ufunc);
3866
3867 if (debug_break_level > 0 || may_break_in_function(ufunc))
3868 return CT_DEBUG;
3869#ifdef FEAT_PROFILE
3870 if (do_profiling == PROF_YES)
3871 {
Ernie Rael21d32122023-09-02 15:09:18 +02003872 if (!ufunc->uf_profiling && has_profiling(FALSE, ufunc->uf_name, NULL,
3873 &ufunc->uf_hash))
Bram Moolenaar139575d2022-03-15 19:29:30 +00003874 func_do_profile(ufunc);
3875 if (ufunc->uf_profiling)
3876 return CT_PROFILE;
3877 }
3878#endif
3879 return CT_NONE;
3880}
3881
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01003882/*
3883 * Free the compiled instructions saved for a def function. This is used when
3884 * compiling a def function and the function was compiled before.
3885 * The index is reused.
3886 */
3887 static void
3888clear_def_function(ufunc_T *ufunc, compiletype_T compile_type)
3889{
3890 isn_T *instr_dest = NULL;
3891 dfunc_T *dfunc;
3892
3893 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
3894
3895 switch (compile_type)
3896 {
3897 case CT_PROFILE:
3898#ifdef FEAT_PROFILE
3899 instr_dest = dfunc->df_instr_prof; break;
3900#endif
3901 case CT_NONE: instr_dest = dfunc->df_instr; break;
3902 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
3903 }
3904
3905 if (instr_dest != NULL)
3906 // Was compiled in this mode before: Free old instructions.
3907 delete_def_function_contents(dfunc, FALSE);
3908
3909 ga_clear_strings(&dfunc->df_var_names);
3910 dfunc->df_defer_var_idx = 0;
3911}
Bram Moolenaar7b829262021-10-13 15:04:34 +01003912
3913/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02003914 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003915 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02003916 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02003917 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02003918add_def_function(ufunc_T *ufunc)
3919{
3920 dfunc_T *dfunc;
3921
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003922 if (def_functions.ga_len == 0)
3923 {
3924 // The first position is not used, so that a zero uf_dfunc_idx means it
3925 // wasn't set.
Bram Moolenaar35578162021-08-02 19:10:38 +02003926 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003927 return FAIL;
3928 ++def_functions.ga_len;
3929 }
3930
Bram Moolenaar09689a02020-05-09 22:50:08 +02003931 // Add the function to "def_functions".
Bram Moolenaar35578162021-08-02 19:10:38 +02003932 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar09689a02020-05-09 22:50:08 +02003933 return FAIL;
3934 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
3935 CLEAR_POINTER(dfunc);
3936 dfunc->df_idx = def_functions.ga_len;
3937 ufunc->uf_dfunc_idx = dfunc->df_idx;
3938 dfunc->df_ufunc = ufunc;
John Marriottb32800f2025-02-01 15:25:34 +01003939 dfunc->df_name = vim_strnsave(ufunc->uf_name, ufunc->uf_namelen);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02003940 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003941 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02003942 ++def_functions.ga_len;
3943 return OK;
3944}
3945
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01003946 static int
3947compile_dfunc_ufunc_init(
3948 ufunc_T *ufunc,
3949 cctx_T *outer_cctx,
3950 compiletype_T compile_type,
3951 int *new_def_function)
3952{
3953 // When using a function that was compiled before: Free old instructions.
3954 // The index is reused. Otherwise add a new entry in "def_functions".
3955 if (ufunc->uf_dfunc_idx > 0)
3956 clear_def_function(ufunc, compile_type);
3957 else
3958 {
3959 if (add_def_function(ufunc) == FAIL)
3960 return FAIL;
3961
3962 *new_def_function = TRUE;
3963 }
3964
3965 if ((ufunc->uf_flags & FC_CLOSURE) && outer_cctx == NULL)
3966 {
3967 semsg(_(e_compiling_closure_without_context_str),
3968 printable_func_name(ufunc));
3969 return FAIL;
3970 }
3971
3972 ufunc->uf_def_status = UF_COMPILING;
3973
3974 return OK;
3975}
3976
3977/*
3978 * Initialize the compilation context for compiling a def function.
3979 */
3980 static void
3981compile_dfunc_cctx_init(
3982 cctx_T *cctx,
3983 cctx_T *outer_cctx,
3984 ufunc_T *ufunc,
3985 compiletype_T compile_type)
3986{
3987 CLEAR_FIELD(*cctx);
3988
3989 cctx->ctx_compile_type = compile_type;
3990 cctx->ctx_ufunc = ufunc;
3991 cctx->ctx_lnum = -1;
3992 cctx->ctx_outer = outer_cctx;
3993 ga_init2(&cctx->ctx_locals, sizeof(lvar_T), 10);
3994 // Each entry on the type stack consists of two type pointers.
3995 ga_init2(&cctx->ctx_type_stack, sizeof(type2_T), 50);
3996 cctx->ctx_type_list = &ufunc->uf_type_list;
3997 ga_init2(&cctx->ctx_instr, sizeof(isn_T), 50);
3998}
3999
Bram Moolenaar09689a02020-05-09 22:50:08 +02004000/*
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004001 * For an object constructor, generate instruction to setup "this" (the first
4002 * local variable) and to initialize the object variables.
4003 */
4004 static int
4005obj_constructor_prologue(ufunc_T *ufunc, cctx_T *cctx)
4006{
4007 generate_CONSTRUCT(cctx, ufunc->uf_class);
4008
4009 for (int i = 0; i < ufunc->uf_class->class_obj_member_count; ++i)
4010 {
4011 ocmember_T *m = &ufunc->uf_class->class_obj_members[i];
4012
4013 if (i < 2 && IS_ENUM(ufunc->uf_class))
4014 // The first two object variables in an enum are the name
4015 // and the ordinal. These are set by the ISN_CONSTRUCT
4016 // instruction. So don't generate instructions to set
4017 // these variables.
4018 continue;
4019
4020 if (m->ocm_init != NULL)
4021 {
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +01004022 char_u *expr = m->ocm_init;
4023 sctx_T save_current_sctx;
4024 int change_sctx = FALSE;
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004025
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +01004026 // If the member variable initialization script context is
4027 // different from the current script context, then change it.
4028 if (current_sctx.sc_sid != m->ocm_init_sctx.sc_sid)
4029 change_sctx = TRUE;
4030
4031 if (change_sctx)
4032 {
4033 // generate an instruction to change the script context to the
4034 // member variable initialization script context.
4035 save_current_sctx = current_sctx;
4036 current_sctx = m->ocm_init_sctx;
4037 generate_SCRIPTCTX_SET(cctx, current_sctx);
4038 }
4039
4040 int r = compile_expr0(&expr, cctx);
4041
4042 if (change_sctx)
4043 {
4044 // restore the previous script context
4045 current_sctx = save_current_sctx;
4046 generate_SCRIPTCTX_SET(cctx, current_sctx);
4047 }
4048
4049 if (r == FAIL)
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004050 return FAIL;
4051
4052 if (!ends_excmd2(m->ocm_init, expr))
4053 {
4054 semsg(_(e_trailing_characters_str), expr);
4055 return FAIL;
4056 }
4057
4058 type_T *type = get_type_on_stack(cctx, 0);
4059 if (m->ocm_type->tt_type == VAR_ANY
4060 && !(m->ocm_flags & OCMFLAG_HAS_TYPE)
4061 && type->tt_type != VAR_SPECIAL)
4062 {
4063 // If the member variable type is not yet set, then use
4064 // the initialization expression type.
4065 m->ocm_type = type;
4066 }
LemonBoyf4af3312024-07-04 13:43:12 +02004067 else
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004068 {
4069 // The type of the member initialization expression is
4070 // determined at run time. Add a runtime type check.
4071 where_T where = WHERE_INIT;
4072 where.wt_kind = WT_MEMBER;
4073 where.wt_func_name = (char *)m->ocm_name;
4074 if (need_type_where(type, m->ocm_type, FALSE, -1,
4075 where, cctx, FALSE, FALSE) == FAIL)
4076 return FAIL;
4077 }
4078 }
4079 else
4080 push_default_value(cctx, m->ocm_type->tt_type, FALSE, NULL);
4081
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01004082 if (((m->ocm_type->tt_type == VAR_DICT
4083 || m->ocm_type->tt_type == VAR_LIST)
4084 && m->ocm_type->tt_member != NULL
4085 && m->ocm_type->tt_member != &t_any
4086 && m->ocm_type->tt_member != &t_unknown)
4087 || (m->ocm_type->tt_type == VAR_TUPLE
4088 && m->ocm_type->tt_argcount > 0))
4089 // Set the type in the list, tuple or dict, so that it can be
4090 // checked, also in legacy script.
LemonBoyf4af3312024-07-04 13:43:12 +02004091 generate_SETTYPE(cctx, m->ocm_type);
4092
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004093 generate_STORE_THIS(cctx, i);
4094 }
4095
4096 return OK;
4097}
4098
4099/*
4100 * For an object method and an constructor, generate instruction to setup
4101 * "this" (the first local variable). For a constructor, generate instructions
4102 * to initialize the object variables.
4103 */
4104 static int
4105obj_method_prologue(ufunc_T *ufunc, cctx_T *cctx)
4106{
4107 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
4108
4109 if (GA_GROW_FAILS(&dfunc->df_var_names, 1))
4110 return FAIL;
4111
4112 ((char_u **)dfunc->df_var_names.ga_data)[0] =
4113 vim_strsave((char_u *)"this");
4114 ++dfunc->df_var_names.ga_len;
4115
4116 // In the constructor allocate memory for the object and initialize the
4117 // object members.
4118 if (IS_CONSTRUCTOR_METHOD(ufunc))
4119 return obj_constructor_prologue(ufunc, cctx);
4120
4121 return OK;
4122}
4123
4124/*
4125 * Produce instructions for the default values of optional arguments.
4126 */
4127 static int
4128compile_def_function_default_args(
4129 ufunc_T *ufunc,
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004130 garray_T *instr,
4131 cctx_T *cctx)
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004132{
4133 int count = ufunc->uf_def_args.ga_len;
4134 int first_def_arg = ufunc->uf_args.ga_len - count;
4135 int i;
4136 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
4137 int did_set_arg_type = FALSE;
4138
4139 // Produce instructions for the default values of optional arguments.
4140 SOURCING_LNUM = 0; // line number unknown
4141 for (i = 0; i < count; ++i)
4142 {
4143 char_u *arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
4144 if (STRCMP(arg, "v:none") == 0)
4145 // "arg = v:none" means the argument is optional without
4146 // setting a value when the argument is missing.
4147 continue;
4148
4149 type_T *val_type;
4150 int arg_idx = first_def_arg + i;
4151 where_T where = WHERE_INIT;
4152 int jump_instr_idx = instr->ga_len;
4153 isn_T *isn;
4154
4155 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
4156 if (generate_JUMP_IF_ARG(cctx, ISN_JUMP_IF_ARG_SET,
4157 i - count - off) == FAIL)
4158 return FAIL;
4159
4160 // Make sure later arguments are not found.
4161 ufunc->uf_args_visible = arg_idx;
4162
4163 int r = compile_expr0(&arg, cctx);
4164 if (r == FAIL)
4165 return FAIL;
4166
4167 // If no type specified use the type of the default value.
4168 // Otherwise check that the default value type matches the
4169 // specified type.
4170 val_type = get_type_on_stack(cctx, 0);
4171 where.wt_index = arg_idx + 1;
4172 where.wt_kind = WT_ARGUMENT;
4173 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
4174 {
4175 did_set_arg_type = TRUE;
4176 ufunc->uf_arg_types[arg_idx] = val_type;
4177 }
4178 else if (need_type_where(val_type, ufunc->uf_arg_types[arg_idx],
4179 FALSE, -1, where, cctx, FALSE, FALSE) == FAIL)
4180 return FAIL;
4181
4182 if (generate_STORE(cctx, ISN_STORE, i - count - off, NULL) == FAIL)
4183 return FAIL;
4184
4185 // set instruction index in JUMP_IF_ARG_SET to here
4186 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
4187 isn->isn_arg.jumparg.jump_where = instr->ga_len;
4188 }
4189
4190 if (did_set_arg_type)
4191 set_function_type(ufunc);
4192
4193 return OK;
4194}
4195
4196/*
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004197 * Compile def function body. Loop over all the lines in the function and
4198 * generate instructions.
4199 */
4200 static int
4201compile_def_function_body(
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004202 int last_func_lnum,
4203 int check_return_type,
4204 garray_T *lines_to_free,
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004205 char **errormsg,
4206 cctx_T *cctx)
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004207{
4208 char_u *line = NULL;
4209 char_u *p;
4210 int did_emsg_before = did_emsg;
4211#ifdef FEAT_PROFILE
4212 int prof_lnum = -1;
4213#endif
4214 int debug_lnum = -1;
4215
4216 for (;;)
4217 {
4218 exarg_T ea;
4219 int starts_with_colon = FALSE;
4220 char_u *cmd;
4221 cmdmod_T local_cmdmod;
4222
4223 // Bail out on the first error to avoid a flood of errors and report
4224 // the right line number when inside try/catch.
4225 if (did_emsg_before != did_emsg)
4226 return FAIL;
4227
4228 if (line != NULL && *line == '|')
4229 // the line continues after a '|'
4230 ++line;
4231 else if (line != NULL && *skipwhite(line) != NUL
4232 && !(*line == '#' && (line == cctx->ctx_line_start
4233 || VIM_ISWHITE(line[-1]))))
4234 {
4235 semsg(_(e_trailing_characters_str), line);
4236 return FAIL;
4237 }
4238 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
4239 return FAIL;
4240 else
4241 {
4242 line = next_line_from_context(cctx, FALSE);
4243 if (cctx->ctx_lnum >= last_func_lnum)
4244 {
4245 // beyond the last line
4246#ifdef FEAT_PROFILE
4247 if (cctx->ctx_skip != SKIP_YES)
4248 may_generate_prof_end(cctx, prof_lnum);
4249#endif
4250 break;
4251 }
4252 // Make a copy, splitting off nextcmd and removing trailing spaces
4253 // may change it.
4254 if (line != NULL)
4255 {
4256 line = vim_strsave(line);
4257 if (ga_add_string(lines_to_free, line) == FAIL)
4258 return FAIL;
4259 }
4260 }
4261
4262 CLEAR_FIELD(ea);
4263 ea.cmdlinep = &line;
4264 ea.cmd = skipwhite(line);
4265 ea.skip = cctx->ctx_skip == SKIP_YES;
4266
4267 if (*ea.cmd == '#')
4268 {
4269 // "#" starts a comment, but "#{" is an error
4270 if (vim9_bad_comment(ea.cmd))
4271 return FAIL;
4272 line = (char_u *)"";
4273 continue;
4274 }
4275
4276#ifdef FEAT_PROFILE
4277 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_lnum != prof_lnum
4278 && cctx->ctx_skip != SKIP_YES)
4279 {
4280 may_generate_prof_end(cctx, prof_lnum);
4281
4282 prof_lnum = cctx->ctx_lnum;
4283 generate_instr(cctx, ISN_PROF_START);
4284 }
4285#endif
4286 if (cctx->ctx_compile_type == CT_DEBUG && cctx->ctx_lnum != debug_lnum
4287 && cctx->ctx_skip != SKIP_YES)
4288 {
4289 debug_lnum = cctx->ctx_lnum;
4290 generate_instr_debug(cctx);
4291 }
4292 cctx->ctx_prev_lnum = cctx->ctx_lnum + 1;
4293
4294 // Some things can be recognized by the first character.
4295 switch (*ea.cmd)
4296 {
4297 case '}':
4298 {
4299 // "}" ends a block scope
4300 scopetype_T stype = cctx->ctx_scope == NULL
4301 ? NO_SCOPE : cctx->ctx_scope->se_type;
4302
4303 if (stype == BLOCK_SCOPE)
4304 {
4305 compile_endblock(cctx);
4306 line = ea.cmd;
4307 }
4308 else
4309 {
4310 emsg(_(e_using_rcurly_outside_if_block_scope));
4311 return FAIL;
4312 }
4313 if (line != NULL)
4314 line = skipwhite(ea.cmd + 1);
4315 continue;
4316 }
4317
4318 case '{':
4319 // "{" starts a block scope
4320 // "{'a': 1}->func() is something else
4321 if (ends_excmd(*skipwhite(ea.cmd + 1)))
4322 {
4323 line = compile_block(ea.cmd, cctx);
4324 continue;
4325 }
4326 break;
4327 }
4328
4329 /*
4330 * COMMAND MODIFIERS
4331 */
4332 cctx->ctx_has_cmdmod = FALSE;
4333 if (parse_command_modifiers(&ea, errormsg, &local_cmdmod, FALSE)
4334 == FAIL)
4335 return FAIL;
4336 generate_cmdmods(cctx, &local_cmdmod);
4337 undo_cmdmod(&local_cmdmod);
4338
4339 // Check if there was a colon after the last command modifier or before
4340 // the current position.
4341 for (p = ea.cmd; p >= line; --p)
4342 {
4343 if (*p == ':')
4344 starts_with_colon = TRUE;
4345 if (p < ea.cmd && !VIM_ISWHITE(*p))
4346 break;
4347 }
4348
4349 // Skip ":call" to get to the function name, unless using :legacy
4350 p = ea.cmd;
4351 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
4352 {
4353 if (checkforcmd(&ea.cmd, "call", 3))
4354 {
4355 if (*ea.cmd == '(')
4356 // not for "call()"
4357 ea.cmd = p;
4358 else
4359 ea.cmd = skipwhite(ea.cmd);
4360 }
4361
4362 if (!starts_with_colon)
4363 {
4364 int assign;
4365
4366 // Check for assignment after command modifiers.
4367 assign = may_compile_assignment(&ea, &line, cctx);
4368 if (assign == OK)
4369 goto nextline;
4370 if (assign == FAIL)
4371 return FAIL;
4372 }
4373 }
4374
4375 /*
4376 * COMMAND after range
4377 * 'text'->func() should not be confused with 'a mark
4378 * 0z1234->func() should not be confused with a zero line number
4379 * "++nr" and "--nr" are eval commands
4380 * in "$ENV->func()" the "$" is not a range
4381 * "123->func()" is a method call
4382 */
4383 cmd = ea.cmd;
4384 if ((*cmd != '$' || starts_with_colon)
4385 && (starts_with_colon
4386 || !(*cmd == '\''
4387 || (cmd[0] == '0' && cmd[1] == 'z')
4388 || (cmd[0] != NUL && cmd[0] == cmd[1]
4389 && (*cmd == '+' || *cmd == '-'))
4390 || number_method(cmd))))
4391 {
4392 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
4393 if (ea.cmd > cmd)
4394 {
4395 if (!starts_with_colon
4396 && !(local_cmdmod.cmod_flags & CMOD_LEGACY))
4397 {
4398 semsg(_(e_colon_required_before_range_str), cmd);
4399 return FAIL;
4400 }
4401 ea.addr_count = 1;
4402 if (ends_excmd2(line, ea.cmd))
4403 {
4404 // A range without a command: jump to the line.
4405 generate_EXEC(cctx, ISN_EXECRANGE,
4406 vim_strnsave(cmd, ea.cmd - cmd));
4407 line = ea.cmd;
4408 goto nextline;
4409 }
4410 }
4411 }
4412 p = find_ex_command(&ea, NULL,
4413 starts_with_colon || (local_cmdmod.cmod_flags & CMOD_LEGACY)
4414 ? NULL : item_exists, cctx);
4415
4416 if (p == NULL)
4417 {
4418 if (cctx->ctx_skip != SKIP_YES)
4419 semsg(_(e_ambiguous_use_of_user_defined_command_str), ea.cmd);
4420 return FAIL;
4421 }
4422
4423 // When using ":legacy cmd" always use compile_exec().
4424 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
4425 {
4426 char_u *start = ea.cmd;
4427
4428 switch (ea.cmdidx)
4429 {
4430 case CMD_if:
4431 case CMD_elseif:
4432 case CMD_else:
4433 case CMD_endif:
4434 case CMD_for:
4435 case CMD_endfor:
4436 case CMD_continue:
4437 case CMD_break:
4438 case CMD_while:
4439 case CMD_endwhile:
4440 case CMD_try:
4441 case CMD_catch:
4442 case CMD_finally:
4443 case CMD_endtry:
4444 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
4445 return FAIL;
4446 default: break;
4447 }
4448
4449 // ":legacy return expr" needs to be handled differently.
4450 if (checkforcmd(&start, "return", 4))
4451 ea.cmdidx = CMD_return;
4452 else
4453 ea.cmdidx = CMD_legacy;
4454 }
4455
4456 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
4457 {
4458 // "eval" is used for "val->func()" and "var" for "var = val", then
4459 // "p" is equal to "ea.cmd" for a valid command.
4460 if (ea.cmdidx == CMD_eval || ea.cmdidx == CMD_var)
4461 ;
4462 else if (cctx->ctx_skip == SKIP_YES)
4463 {
4464 line += STRLEN(line);
4465 goto nextline;
4466 }
4467 else
4468 {
4469 semsg(_(e_command_not_recognized_str), ea.cmd);
4470 return FAIL;
4471 }
4472 }
4473
4474 if ((cctx->ctx_had_return || cctx->ctx_had_throw)
4475 && ea.cmdidx != CMD_elseif
4476 && ea.cmdidx != CMD_else
4477 && ea.cmdidx != CMD_endif
4478 && ea.cmdidx != CMD_endfor
4479 && ea.cmdidx != CMD_endwhile
4480 && ea.cmdidx != CMD_catch
4481 && ea.cmdidx != CMD_finally
4482 && ea.cmdidx != CMD_endtry
4483 && !ignore_unreachable_code_for_testing)
4484 {
4485 semsg(_(e_unreachable_code_after_str),
4486 cctx->ctx_had_return ? "return" : "throw");
4487 return FAIL;
4488 }
Yegappan Lakshmananab9a8942024-12-30 09:56:34 +01004489
4490 // When processing the end of an if-else block, don't clear the
4491 // "ctx_had_throw" flag. If an if-else block ends in a "throw"
4492 // statement, then it is considered to end in a "return" statement.
4493 // The "ctx_had_throw" is cleared immediately after processing the
4494 // if-else block ending statement.
4495 // Otherwise, clear the "had_throw" flag.
4496 if (ea.cmdidx != CMD_else && ea.cmdidx != CMD_elseif
4497 && ea.cmdidx != CMD_endif)
4498 cctx->ctx_had_throw = FALSE;
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004499
4500 p = skipwhite(p);
4501 if (ea.cmdidx != CMD_SIZE
4502 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
4503 {
4504 if (ea.cmdidx >= 0)
4505 ea.argt = excmd_get_argt(ea.cmdidx);
4506 if ((ea.argt & EX_BANG) && *p == '!')
4507 {
4508 ea.forceit = TRUE;
4509 p = skipwhite(p + 1);
4510 }
4511 if ((ea.argt & EX_RANGE) == 0 && ea.addr_count > 0)
4512 {
4513 emsg(_(e_no_range_allowed));
4514 return FAIL;
4515 }
4516 }
4517
4518 switch (ea.cmdidx)
4519 {
4520 case CMD_def:
4521 case CMD_function:
4522 ea.arg = p;
4523 line = compile_nested_function(&ea, cctx, lines_to_free);
4524 break;
4525
4526 case CMD_return:
4527 line = compile_return(p, check_return_type,
4528 local_cmdmod.cmod_flags & CMOD_LEGACY, cctx);
4529 cctx->ctx_had_return = TRUE;
4530 break;
4531
4532 case CMD_let:
4533 emsg(_(e_cannot_use_let_in_vim9_script));
4534 break;
4535 case CMD_var:
4536 case CMD_final:
4537 case CMD_const:
4538 case CMD_increment:
4539 case CMD_decrement:
4540 line = compile_assignment(p, &ea, ea.cmdidx, cctx);
4541 if (line == p)
4542 {
4543 emsg(_(e_invalid_assignment));
4544 line = NULL;
4545 }
4546 break;
4547
4548 case CMD_unlet:
4549 case CMD_unlockvar:
4550 case CMD_lockvar:
4551 line = compile_unletlock(p, &ea, cctx);
4552 break;
4553
4554 case CMD_import:
4555 emsg(_(e_import_can_only_be_used_in_script));
4556 line = NULL;
4557 break;
4558
4559 case CMD_if:
4560 line = compile_if(p, cctx);
4561 break;
4562 case CMD_elseif:
4563 line = compile_elseif(p, cctx);
4564 cctx->ctx_had_return = FALSE;
Yegappan Lakshmananab9a8942024-12-30 09:56:34 +01004565 cctx->ctx_had_throw = FALSE;
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004566 break;
4567 case CMD_else:
4568 line = compile_else(p, cctx);
4569 cctx->ctx_had_return = FALSE;
Yegappan Lakshmananab9a8942024-12-30 09:56:34 +01004570 cctx->ctx_had_throw = FALSE;
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004571 break;
4572 case CMD_endif:
4573 line = compile_endif(p, cctx);
Yegappan Lakshmananab9a8942024-12-30 09:56:34 +01004574 cctx->ctx_had_throw = FALSE;
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004575 break;
4576
4577 case CMD_while:
4578 line = compile_while(p, cctx);
4579 break;
4580 case CMD_endwhile:
4581 line = compile_endwhile(p, cctx);
4582 cctx->ctx_had_return = FALSE;
4583 break;
4584
4585 case CMD_for:
4586 line = compile_for(p, cctx);
4587 break;
4588 case CMD_endfor:
4589 line = compile_endfor(p, cctx);
4590 cctx->ctx_had_return = FALSE;
4591 break;
4592 case CMD_continue:
4593 line = compile_continue(p, cctx);
4594 break;
4595 case CMD_break:
4596 line = compile_break(p, cctx);
4597 break;
4598
4599 case CMD_try:
4600 line = compile_try(p, cctx);
4601 break;
4602 case CMD_catch:
4603 line = compile_catch(p, cctx);
4604 cctx->ctx_had_return = FALSE;
4605 break;
4606 case CMD_finally:
4607 line = compile_finally(p, cctx);
4608 cctx->ctx_had_return = FALSE;
4609 break;
4610 case CMD_endtry:
4611 line = compile_endtry(p, cctx);
4612 break;
4613 case CMD_throw:
4614 line = compile_throw(p, cctx);
4615 cctx->ctx_had_throw = TRUE;
4616 break;
4617
4618 case CMD_eval:
4619 line = compile_eval(p, cctx);
4620 break;
4621
4622 case CMD_defer:
4623 line = compile_defer(p, cctx);
4624 break;
4625
4626#ifdef HAS_MESSAGE_WINDOW
4627 case CMD_echowindow:
4628 {
4629 long cmd_count = get_cmd_count(line, &ea);
4630 if (cmd_count < 0)
4631 line = NULL;
4632 else
4633 line = compile_mult_expr(p, ea.cmdidx,
4634 cmd_count, cctx);
4635 }
4636 break;
4637#endif
4638 case CMD_echo:
4639 case CMD_echon:
4640 case CMD_echoconsole:
4641 case CMD_echoerr:
4642 case CMD_echomsg:
4643 case CMD_execute:
4644 line = compile_mult_expr(p, ea.cmdidx, 0, cctx);
4645 break;
4646
4647 case CMD_put:
4648 ea.cmd = cmd;
64-bitmane08f10a2025-03-18 22:14:34 +01004649 line = compile_put(p, &ea, cctx, FALSE);
4650 break;
4651
4652 case CMD_iput:
4653 ea.cmd = cmd;
4654 line = compile_put(p, &ea, cctx, TRUE);
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004655 break;
4656
4657 case CMD_substitute:
4658 if (check_global_and_subst(ea.cmd, p) == FAIL)
4659 return FAIL;
4660 if (cctx->ctx_skip == SKIP_YES)
4661 line = (char_u *)"";
4662 else
4663 {
4664 ea.arg = p;
4665 line = compile_substitute(line, &ea, cctx);
4666 }
4667 break;
4668
4669 case CMD_redir:
4670 ea.arg = p;
4671 line = compile_redir(line, &ea, cctx);
4672 break;
4673
4674 case CMD_cexpr:
4675 case CMD_lexpr:
4676 case CMD_caddexpr:
4677 case CMD_laddexpr:
4678 case CMD_cgetexpr:
4679 case CMD_lgetexpr:
4680#ifdef FEAT_QUICKFIX
4681 ea.arg = p;
4682 line = compile_cexpr(line, &ea, cctx);
4683#else
4684 ex_ni(&ea);
4685 line = NULL;
4686#endif
4687 break;
4688
4689 case CMD_append:
4690 case CMD_change:
4691 case CMD_insert:
4692 case CMD_k:
4693 case CMD_t:
4694 case CMD_xit:
4695 not_in_vim9(&ea);
4696 return FAIL;
4697
4698 case CMD_SIZE:
4699 if (cctx->ctx_skip != SKIP_YES)
4700 {
4701 semsg(_(e_invalid_command_str), ea.cmd);
4702 return FAIL;
4703 }
4704 // We don't check for a next command here.
4705 line = (char_u *)"";
4706 break;
4707
4708 case CMD_lua:
4709 case CMD_mzscheme:
4710 case CMD_perl:
4711 case CMD_py3:
4712 case CMD_python3:
4713 case CMD_python:
4714 case CMD_pythonx:
4715 case CMD_ruby:
4716 case CMD_tcl:
4717 ea.arg = p;
4718 if (vim_strchr(line, '\n') == NULL)
4719 line = compile_exec(line, &ea, cctx);
4720 else
4721 // heredoc lines have been concatenated with NL
4722 // characters in get_function_body()
4723 line = compile_script(line, cctx);
4724 break;
4725
4726 case CMD_vim9script:
4727 if (cctx->ctx_skip != SKIP_YES)
4728 {
4729 emsg(_(e_vim9script_can_only_be_used_in_script));
4730 return FAIL;
4731 }
4732 line = (char_u *)"";
4733 break;
4734
4735 case CMD_class:
4736 emsg(_(e_class_can_only_be_used_in_script));
4737 return FAIL;
4738
4739 case CMD_type:
4740 emsg(_(e_type_can_only_be_used_in_script));
4741 return FAIL;
4742
4743 case CMD_global:
4744 if (check_global_and_subst(ea.cmd, p) == FAIL)
4745 return FAIL;
4746 // FALLTHROUGH
4747 default:
4748 // Not recognized, execute with do_cmdline_cmd().
4749 ea.arg = p;
4750 line = compile_exec(line, &ea, cctx);
4751 break;
4752 }
4753nextline:
4754 if (line == NULL)
4755 return FAIL;
4756 line = skipwhite(line);
4757
4758 // Undo any command modifiers.
4759 generate_undo_cmdmods(cctx);
4760
4761 if (cctx->ctx_type_stack.ga_len < 0)
4762 {
4763 iemsg("Type stack underflow");
4764 return FAIL;
4765 }
4766 } // END of the loop over all the function body lines.
4767
4768 return OK;
4769}
4770
4771/*
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004772 * Returns TRUE if the end of a scope (if, while, for, block) is missing.
4773 * Called after compiling a def function body.
4774 */
4775 static int
4776compile_dfunc_scope_end_missing(cctx_T *cctx)
4777{
4778 if (cctx->ctx_scope == NULL)
4779 return FALSE;
4780
4781 if (cctx->ctx_scope->se_type == IF_SCOPE)
4782 emsg(_(e_missing_endif));
4783 else if (cctx->ctx_scope->se_type == WHILE_SCOPE)
4784 emsg(_(e_missing_endwhile));
4785 else if (cctx->ctx_scope->se_type == FOR_SCOPE)
4786 emsg(_(e_missing_endfor));
4787 else
4788 emsg(_(e_missing_rcurly));
4789
4790 return TRUE;
4791}
4792
4793/*
Yegappan Lakshmananab9a8942024-12-30 09:56:34 +01004794 * When compiling a def function, if it doesn't have an explicit return
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004795 * statement, then generate a default return instruction. For an object
4796 * constructor, return the object.
4797 */
4798 static int
4799compile_dfunc_generate_default_return(ufunc_T *ufunc, cctx_T *cctx)
4800{
4801 // TODO: if a function ends in "throw" but there was a return elsewhere we
4802 // should not assume the return type is "void".
4803 if (cctx->ctx_had_return || cctx->ctx_had_throw)
4804 return OK;
4805
4806 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
4807 ufunc->uf_ret_type = &t_void;
4808 else if (ufunc->uf_ret_type->tt_type != VAR_VOID
4809 && !IS_CONSTRUCTOR_METHOD(ufunc))
4810 {
4811 emsg(_(e_missing_return_statement));
4812 return FAIL;
4813 }
4814
4815 // Return void if there is no return at the end.
4816 // For a constructor return the object.
4817 if (IS_CONSTRUCTOR_METHOD(ufunc))
4818 {
4819 generate_instr(cctx, ISN_RETURN_OBJECT);
4820 ufunc->uf_ret_type = &ufunc->uf_class->class_object_type;
4821 }
4822 else
4823 generate_instr(cctx, ISN_RETURN_VOID);
4824
4825 return OK;
4826}
4827
4828/*
4829 * Perform the chores after successfully compiling a def function.
4830 */
4831 static void
4832compile_dfunc_epilogue(
4833 cctx_T *outer_cctx,
4834 ufunc_T *ufunc,
4835 garray_T *instr,
4836 cctx_T *cctx)
4837{
4838 dfunc_T *dfunc;
4839
4840 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
4841 dfunc->df_deleted = FALSE;
4842 dfunc->df_script_seq = current_sctx.sc_seq;
4843
4844#ifdef FEAT_PROFILE
4845 if (cctx->ctx_compile_type == CT_PROFILE)
4846 {
4847 dfunc->df_instr_prof = instr->ga_data;
4848 dfunc->df_instr_prof_count = instr->ga_len;
4849 }
4850 else
4851#endif
4852 if (cctx->ctx_compile_type == CT_DEBUG)
4853 {
4854 dfunc->df_instr_debug = instr->ga_data;
4855 dfunc->df_instr_debug_count = instr->ga_len;
4856 }
4857 else
4858 {
4859 dfunc->df_instr = instr->ga_data;
4860 dfunc->df_instr_count = instr->ga_len;
4861 }
4862 dfunc->df_varcount = dfunc->df_var_names.ga_len;
4863 dfunc->df_has_closure = cctx->ctx_has_closure;
4864
4865 if (cctx->ctx_outer_used)
4866 {
4867 ufunc->uf_flags |= FC_CLOSURE;
4868 if (outer_cctx != NULL)
4869 ++outer_cctx->ctx_closure_count;
4870 }
4871
4872 ufunc->uf_def_status = UF_COMPILED;
4873}
4874
4875/*
4876 * Perform the cleanup when a def function compilation fails.
4877 */
4878 static void
4879compile_dfunc_ufunc_cleanup(
4880 ufunc_T *ufunc,
4881 garray_T *instr,
4882 int new_def_function,
4883 char *errormsg,
4884 int did_emsg_before,
4885 cctx_T *cctx)
4886{
4887 dfunc_T *dfunc;
4888
4889 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
4890
4891 // Compiling aborted, free the generated instructions.
4892 clear_instr_ga(instr);
4893 VIM_CLEAR(dfunc->df_name);
4894 ga_clear_strings(&dfunc->df_var_names);
4895
4896 // If using the last entry in the table and it was added above, we
4897 // might as well remove it.
4898 if (!dfunc->df_deleted && new_def_function
4899 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
4900 {
4901 --def_functions.ga_len;
4902 ufunc->uf_dfunc_idx = 0;
4903 }
4904 ufunc->uf_def_status = UF_COMPILE_ERROR;
4905
4906 while (cctx->ctx_scope != NULL)
4907 drop_scope(cctx);
4908
4909 if (errormsg != NULL)
4910 emsg(errormsg);
4911 else if (did_emsg == did_emsg_before)
4912 emsg(_(e_compiling_def_function_failed));
4913}
4914
4915/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004916 * After ex_function() has collected all the function lines: parse and compile
4917 * the lines into instructions.
4918 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004919 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
4920 * the return statement (used for lambda). When uf_ret_type is already set
4921 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01004922 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004923 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02004924 * This can be used recursively through compile_lambda(), which may reallocate
4925 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02004926 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004927 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02004928 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01004929compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02004930 ufunc_T *ufunc,
4931 int check_return_type,
4932 compiletype_T compile_type,
4933 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004934{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004935 garray_T lines_to_free;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004936 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004937 cctx_T cctx;
4938 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01004939 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02004940 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004941 int ret = FAIL;
4942 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02004943 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02004944 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02004945 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02004946 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004947
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004948 // allocated lines are freed at the end
4949 ga_init2(&lines_to_free, sizeof(char_u *), 50);
4950
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004951 // Initialize the ufunc and the compilation context
4952 if (compile_dfunc_ufunc_init(ufunc, outer_cctx, compile_type,
4953 &new_def_function) == FAIL)
Bram Moolenaar96923b72022-03-15 15:57:04 +00004954 return FAIL;
Bram Moolenaar96923b72022-03-15 15:57:04 +00004955
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004956 compile_dfunc_cctx_init(&cctx, outer_cctx, ufunc, compile_type);
Bram Moolenaar985116a2020-07-12 17:31:09 +02004957
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004958 instr = &cctx.ctx_instr;
4959
Bram Moolenaar25e0f582020-05-25 22:36:50 +02004960 // Set the context to the function, it may be compiled when called from
4961 // another script. Set the script version to the most modern one.
4962 // The line number will be set in next_line_from_context().
4963 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004964 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4965
Bram Moolenaardc4c2302021-04-25 13:54:42 +02004966 // Don't use the flag from ":legacy" here.
4967 cmdmod.cmod_flags &= ~CMOD_LEGACY;
4968
Bram Moolenaar25e0f582020-05-25 22:36:50 +02004969 // Make sure error messages are OK.
4970 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
4971 if (do_estack_push)
4972 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02004973 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02004974
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004975 // Make sure arguments don't shadow variables in the context
Bram Moolenaar9a015112021-12-31 14:06:45 +00004976 if (check_args_shadowing(ufunc, &cctx) == FAIL)
4977 goto erret;
4978
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004979 // For an object method and a constructor generate instructions to
4980 // initialize "this" and the object variables.
Bram Moolenaar574950d2023-01-03 19:08:50 +00004981 if (ufunc->uf_flags & (FC_OBJECT|FC_NEW))
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004982 if (obj_method_prologue(ufunc, &cctx) == FAIL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004983 goto erret;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004984
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004985 if (ufunc->uf_def_args.ga_len > 0)
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004986 if (compile_def_function_default_args(ufunc, instr, &cctx) == FAIL)
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004987 goto erret;
Bram Moolenaare28d9b32021-07-03 18:56:53 +02004988 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004989
Ernie Rael7c92e882025-01-18 17:26:39 +01004990 // Compiling an abstract method or a function in an interface is done to
4991 // get the function type. No code is actually compiled.
4992 if (ufunc->uf_class != NULL && (IS_INTERFACE(ufunc->uf_class)
4993 || IS_ABSTRACT_METHOD(ufunc)))
Bram Moolenaarb8bebd02023-01-30 20:24:23 +00004994 {
4995 ufunc->uf_def_status = UF_NOT_COMPILED;
4996 ret = OK;
4997 goto erret;
4998 }
4999
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02005000 // compile the function body
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01005001 if (compile_def_function_body(ufunc->uf_lines.ga_len, check_return_type,
5002 &lines_to_free, &errormsg, &cctx) == FAIL)
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02005003 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005004
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01005005 if (compile_dfunc_scope_end_missing(&cctx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005006 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005007
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01005008 if (compile_dfunc_generate_default_return(ufunc, &cctx) == FAIL)
5009 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005010
Bram Moolenaar599410c2021-04-10 14:03:43 +02005011 // When compiled with ":silent!" and there was an error don't consider the
5012 // function compiled.
5013 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01005014 compile_dfunc_epilogue(outer_cctx, ufunc, instr, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005015
5016 ret = OK;
5017
5018erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02005019 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005020 {
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01005021 // compilation failed. do cleanup.
5022 compile_dfunc_ufunc_cleanup(ufunc, instr, new_def_function,
5023 errormsg, did_emsg_before, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005024 }
5025
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005026 if (cctx.ctx_redir_lhs.lhs_name != NULL)
5027 {
5028 if (ret == OK)
5029 {
5030 emsg(_(e_missing_redir_end));
5031 ret = FAIL;
5032 }
5033 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02005034 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005035 }
5036
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005037 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02005038 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02005039 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02005040 if (do_estack_push)
5041 estack_pop();
5042
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005043 ga_clear_strings(&lines_to_free);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02005044 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005045 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02005046 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005047}
5048
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005049 void
5050set_function_type(ufunc_T *ufunc)
5051{
5052 int varargs = ufunc->uf_va_name != NULL;
5053 int argcount = ufunc->uf_args.ga_len;
5054
5055 // Create a type for the function, with the return type and any
5056 // argument types.
5057 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
5058 // The type is included in "tt_args".
5059 if (argcount > 0 || varargs)
5060 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01005061 if (ufunc->uf_type_list.ga_itemsize == 0)
5062 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005063 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
5064 argcount, &ufunc->uf_type_list);
5065 // Add argument types to the function type.
5066 if (func_type_add_arg_types(ufunc->uf_func_type,
5067 argcount + varargs,
5068 &ufunc->uf_type_list) == FAIL)
5069 return;
5070 ufunc->uf_func_type->tt_argcount = argcount + varargs;
5071 ufunc->uf_func_type->tt_min_argcount =
5072 argcount - ufunc->uf_def_args.ga_len;
5073 if (ufunc->uf_arg_types == NULL)
5074 {
5075 int i;
5076
5077 // lambda does not have argument types.
5078 for (i = 0; i < argcount; ++i)
5079 ufunc->uf_func_type->tt_args[i] = &t_any;
5080 }
5081 else
5082 mch_memmove(ufunc->uf_func_type->tt_args,
5083 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
5084 if (varargs)
5085 {
5086 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +02005087 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005088 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
5089 }
5090 }
5091 else
5092 // No arguments, can use a predefined type.
5093 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
5094 argcount, &ufunc->uf_type_list);
5095}
5096
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005097/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01005098 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01005099 */
5100 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005101delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01005102{
5103 int idx;
5104
Bram Moolenaard505d172022-12-18 21:42:55 +00005105 // In same cases the instructions may refer to a class in which the
5106 // function is defined and unreferencing the class may call back here
5107 // recursively. Set the df_delete_busy to avoid problems.
5108 if (dfunc->df_delete_busy)
5109 return;
5110 dfunc->df_delete_busy = TRUE;
5111
Bram Moolenaar20431c92020-03-20 18:39:46 +01005112 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005113 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005114
5115 if (dfunc->df_instr != NULL)
5116 {
5117 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
5118 delete_instr(dfunc->df_instr + idx);
5119 VIM_CLEAR(dfunc->df_instr);
5120 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +02005121 if (dfunc->df_instr_debug != NULL)
5122 {
5123 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
5124 delete_instr(dfunc->df_instr_debug + idx);
5125 VIM_CLEAR(dfunc->df_instr_debug);
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +02005126 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +01005127#ifdef FEAT_PROFILE
5128 if (dfunc->df_instr_prof != NULL)
5129 {
5130 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
5131 delete_instr(dfunc->df_instr_prof + idx);
5132 VIM_CLEAR(dfunc->df_instr_prof);
Bram Moolenaarc05fe072021-01-24 21:30:48 +01005133 }
5134#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +01005135
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005136 if (mark_deleted)
5137 dfunc->df_deleted = TRUE;
5138 if (dfunc->df_ufunc != NULL)
5139 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaard505d172022-12-18 21:42:55 +00005140
5141 dfunc->df_delete_busy = FALSE;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005142}
5143
5144/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005145 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01005146 * function, unless another user function still uses it.
5147 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005148 */
5149 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01005150unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005151{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00005152 if (ufunc->uf_dfunc_idx <= 0)
5153 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005154
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00005155 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5156 + ufunc->uf_dfunc_idx;
5157
5158 if (--dfunc->df_refcount <= 0)
5159 delete_def_function_contents(dfunc, TRUE);
5160 ufunc->uf_def_status = UF_NOT_COMPILED;
5161 ufunc->uf_dfunc_idx = 0;
5162 if (dfunc->df_ufunc == ufunc)
5163 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005164}
5165
Bram Moolenaarfdeab652020-09-19 15:16:50 +02005166/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01005167 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02005168 */
5169 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01005170link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02005171{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00005172 if (ufunc->uf_dfunc_idx <= 0)
5173 return;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02005174
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00005175 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5176 + ufunc->uf_dfunc_idx;
5177
5178 ++dfunc->df_refcount;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02005179}
5180
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005181#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01005182/*
5183 * Free all functions defined with ":def".
5184 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005185 void
5186free_def_functions(void)
5187{
Bram Moolenaar20431c92020-03-20 18:39:46 +01005188 int idx;
5189
5190 for (idx = 0; idx < def_functions.ga_len; ++idx)
5191 {
5192 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
5193
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005194 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01005195 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005196 }
5197
5198 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005199}
5200#endif
5201
5202
5203#endif // FEAT_EVAL