blob: f6764831af7b8ccad47b7610c2edc5970e51c21f [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;
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200527 if ((actual->tt_type == VAR_LIST || actual->tt_type == VAR_DICT)
528 && actual->tt_type == expected->tt_type)
529 // This takes care of a nested list or dict.
530 return use_typecheck(actual->tt_member, expected->tt_member);
531 return FALSE;
532}
533
534/*
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200535 * Check that
Bram Moolenaar5e654232020-09-16 15:22:00 +0200536 * - "actual" matches "expected" type or
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200537 * - "actual" is a type that can be "expected" type: add a runtime check; or
538 * - return FAIL.
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200539 * If "actual_is_const" is TRUE then the type won't change at runtime, do not
540 * generate a TYPECHECK.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200541 */
Bram Moolenaara1c51952022-02-02 16:20:26 +0000542 int
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200543need_type_where(
544 type_T *actual,
545 type_T *expected,
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000546 int number_ok, // expect VAR_FLOAT but VAR_NUMBER is OK
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200547 int offset,
548 where_T where,
549 cctx_T *cctx,
550 int silent,
551 int actual_is_const)
552{
Bram Moolenaar44a89772021-12-18 12:31:33 +0000553 int ret;
554
Ernie Raele75fde62023-12-21 17:18:54 +0100555 if (expected->tt_type != VAR_CLASS && expected->tt_type != VAR_TYPEALIAS)
556 {
557 if (check_type_is_value(actual) == FAIL)
558 return FAIL;
559 }
560
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200561 if (expected == &t_bool && actual != &t_bool
562 && (actual->tt_flags & TTFLAG_BOOL_OK))
563 {
564 // Using "0", "1" or the result of an expression with "&&" or "||" as a
565 // boolean is OK but requires a conversion.
566 generate_2BOOL(cctx, FALSE, offset);
567 return OK;
568 }
569
Bram Moolenaar44a89772021-12-18 12:31:33 +0000570 ret = check_type_maybe(expected, actual, FALSE, where);
571 if (ret == OK)
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200572 return OK;
573
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +0000574 // If actual a constant a runtime check makes no sense. If it's
575 // null_function it is OK.
576 if (actual_is_const && ret == MAYBE && actual == &t_func_unknown)
577 return OK;
578
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200579 // If the actual type can be the expected type add a runtime check.
Bram Moolenaarfa46ead2021-12-22 13:18:39 +0000580 if (!actual_is_const && ret == MAYBE && use_typecheck(actual, expected))
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200581 {
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000582 generate_TYPECHECK(cctx, expected, number_ok, offset,
LemonBoyc5d27442023-08-19 13:02:35 +0200583 where.wt_kind == WT_VARIABLE, where.wt_index);
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200584 return OK;
585 }
586
587 if (!silent)
588 type_mismatch_where(expected, actual, where);
589 return FAIL;
590}
591
Bram Moolenaar351ead02021-01-16 16:07:01 +0100592 int
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200593need_type(
594 type_T *actual,
595 type_T *expected,
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000596 int number_ok, // when expected is float number is also OK
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200597 int offset,
Bram Moolenaar351ead02021-01-16 16:07:01 +0100598 int arg_idx,
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200599 cctx_T *cctx,
Bram Moolenaar334a8b42020-10-19 16:07:42 +0200600 int silent,
601 int actual_is_const)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200602{
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200603 where_T where = WHERE_INIT;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100604
LemonBoyc5d27442023-08-19 13:02:35 +0200605 if (arg_idx > 0)
606 {
607 where.wt_index = arg_idx;
608 where.wt_kind = WT_ARGUMENT;
609 }
Bram Moolenaarc6951a72022-12-29 20:56:24 +0000610 return need_type_where(actual, expected, number_ok, offset, where,
Bram Moolenaar4270d8b2021-08-07 16:30:42 +0200611 cctx, silent, actual_is_const);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200612}
613
614/*
Bram Moolenaarfa103972022-09-29 19:14:42 +0100615 * Set type of variable "lvar" to "type". If the variable is a constant then
616 * the type gets TTFLAG_CONST.
617 */
618 static void
619set_var_type(lvar_T *lvar, type_T *type_arg, cctx_T *cctx)
620{
621 type_T *type = type_arg;
622
Bram Moolenaar6586a012022-09-30 11:04:50 +0100623 if (lvar->lv_const == ASSIGN_CONST && (type->tt_flags & TTFLAG_CONST) == 0)
Bram Moolenaarfa103972022-09-29 19:14:42 +0100624 {
625 if (type->tt_flags & TTFLAG_STATIC)
626 // entry in static_types[] is followed by const type
627 type = type + 1;
628 else
629 {
630 type = copy_type(type, cctx->ctx_type_list);
631 type->tt_flags |= TTFLAG_CONST;
632 }
633 }
634 lvar->lv_type = type;
635}
636
637/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100638 * Reserve space for a local variable.
Bram Moolenaar6586a012022-09-30 11:04:50 +0100639 * "assign" can be ASSIGN_VAR for :var, ASSIGN_CONST for :const and
640 * ASSIGN_FINAL for :final.
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200641 * Return the variable or NULL if it failed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100642 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000643 lvar_T *
Bram Moolenaare8211a32020-10-09 22:04:29 +0200644reserve_local(
645 cctx_T *cctx,
646 char_u *name,
647 size_t len,
Bram Moolenaar6586a012022-09-30 11:04:50 +0100648 int assign,
Bram Moolenaare8211a32020-10-09 22:04:29 +0200649 type_T *type)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100650{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100651 lvar_T *lvar;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200652 dfunc_T *dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100653
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200654 if (arg_exists(name, len, NULL, NULL, NULL, cctx) == OK)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100655 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200656 emsg_namelen(_(e_str_is_used_as_argument), name, (int)len);
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200657 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100658 }
659
Bram Moolenaar35578162021-08-02 19:10:38 +0200660 if (GA_GROW_FAILS(&cctx->ctx_locals, 1))
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200661 return NULL;
662 lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + cctx->ctx_locals.ga_len++;
Bram Moolenaare8211a32020-10-09 22:04:29 +0200663 CLEAR_POINTER(lvar);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100664
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200665 // Every local variable uses the next entry on the stack. We could re-use
666 // the last ones when leaving a scope, but then variables used in a closure
667 // might get overwritten. To keep things simple do not re-use stack
668 // entries. This is less efficient, but memory is cheap these days.
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200669 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx;
670 lvar->lv_idx = dfunc->df_var_names.ga_len;
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200671
Bram Moolenaar71ccd032020-06-12 22:59:11 +0200672 lvar->lv_name = vim_strnsave(name, len == 0 ? STRLEN(name) : len);
Bram Moolenaar6586a012022-09-30 11:04:50 +0100673 lvar->lv_const = assign;
Bram Moolenaarfa103972022-09-29 19:14:42 +0100674 if (type == &t_unknown || type == &t_any)
675 // type not known yet, may be inferred from RHS
676 lvar->lv_type = type;
677 else
678 // may use TTFLAG_CONST
679 set_var_type(lvar, type, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100680
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200681 // Remember the name for debugging.
Bram Moolenaar35578162021-08-02 19:10:38 +0200682 if (GA_GROW_FAILS(&dfunc->df_var_names, 1))
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200683 return NULL;
684 ((char_u **)dfunc->df_var_names.ga_data)[lvar->lv_idx] =
685 vim_strsave(lvar->lv_name);
686 ++dfunc->df_var_names.ga_len;
687
Bram Moolenaarb84a3812020-05-01 15:44:29 +0200688 return lvar;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100689}
690
691/*
Bram Moolenaar08251752021-01-11 21:20:18 +0100692 * If "check_writable" is ASSIGN_CONST give an error if the variable was
693 * defined with :final or :const, if "check_writable" is ASSIGN_FINAL give an
694 * error if the variable was defined with :const.
695 */
696 static int
697check_item_writable(svar_T *sv, int check_writable, char_u *name)
698{
699 if ((check_writable == ASSIGN_CONST && sv->sv_const != 0)
700 || (check_writable == ASSIGN_FINAL
701 && sv->sv_const == ASSIGN_CONST))
702 {
Bram Moolenaard8e44472021-07-21 22:20:33 +0200703 semsg(_(e_cannot_change_readonly_variable_str), name);
Bram Moolenaar08251752021-01-11 21:20:18 +0100704 return FAIL;
705 }
706 return OK;
707}
708
709/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100710 * Find "name" in script-local items of script "sid".
Bram Moolenaar08251752021-01-11 21:20:18 +0100711 * Pass "check_writable" to check_item_writable().
Bram Moolenaar7a3b8022022-02-14 19:53:03 +0000712 * "cctx" is NULL at the script level, "cstack" is NULL in a function.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100713 * Returns the index in "sn_var_vals" if found.
714 * If found but not in "sn_var_vals" returns -1.
Bram Moolenaar08251752021-01-11 21:20:18 +0100715 * If not found or the variable is not writable returns -2.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100716 */
717 int
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000718get_script_item_idx(
719 int sid,
720 char_u *name,
721 int check_writable,
722 cctx_T *cctx,
723 cstack_T *cstack)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100724{
725 hashtab_T *ht;
726 dictitem_T *di;
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100727 scriptitem_T *si = SCRIPT_ITEM(sid);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200728 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100729 int idx;
730
Bram Moolenaare3d46852020-08-29 13:39:17 +0200731 if (!SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100732 return -1;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200733 if (sid == current_sctx.sc_sid)
734 {
Bram Moolenaarb6a138e2022-02-08 21:17:22 +0000735 sallvar_T *sav = find_script_var(name, 0, cctx, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200736
737 if (sav == NULL)
738 return -2;
739 idx = sav->sav_var_vals_idx;
740 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar08251752021-01-11 21:20:18 +0100741 if (check_item_writable(sv, check_writable, name) == FAIL)
742 return -2;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200743 return idx;
744 }
745
746 // First look the name up in the hashtable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100747 ht = &SCRIPT_VARS(sid);
748 di = find_var_in_ht(ht, 0, name, TRUE);
749 if (di == NULL)
Bram Moolenaar577bd852022-01-10 18:42:52 +0000750 {
751 if (si->sn_autoload_prefix != NULL)
752 {
753 hashitem_T *hi;
754
755 // A variable exported from an autoload script is in the global
756 // variables, we can find it in the all_vars table.
757 hi = hash_find(&si->sn_all_vars.dv_hashtab, name);
758 if (!HASHITEM_EMPTY(hi))
759 return HI2SAV(hi)->sav_var_vals_idx;
760 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100761 return -2;
Bram Moolenaar577bd852022-01-10 18:42:52 +0000762 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100763
764 // Now find the svar_T index in sn_var_vals.
765 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
766 {
Bram Moolenaarfbbcd002020-10-15 12:46:44 +0200767 sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100768 if (sv->sv_tv == &di->di_tv)
769 {
Bram Moolenaar08251752021-01-11 21:20:18 +0100770 if (check_item_writable(sv, check_writable, name) == FAIL)
771 return -2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100772 return idx;
773 }
774 }
775 return -1;
776}
777
Yegappan Lakshmanan782b43d2022-01-08 18:43:40 +0000778 static imported_T *
779find_imported_in_script(char_u *name, size_t len, int sid)
780{
781 scriptitem_T *si;
782 int idx;
783
784 if (!SCRIPT_ID_VALID(sid))
785 return NULL;
786 si = SCRIPT_ITEM(sid);
787 for (idx = 0; idx < si->sn_imports.ga_len; ++idx)
788 {
789 imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx;
790
791 if (len == 0 ? STRCMP(name, import->imp_name) == 0
792 : STRLEN(import->imp_name) == len
793 && STRNCMP(name, import->imp_name, len) == 0)
794 return import;
795 }
796 return NULL;
797}
798
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100799/*
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000800 * Find "name" in imported items of the current script.
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100801 * If "len" is 0 use any length that works.
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000802 * If "load" is TRUE and the script was not loaded yet, load it now.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100803 */
804 imported_T *
Bram Moolenaar4b1d9632022-02-13 21:51:08 +0000805find_imported(char_u *name, size_t len, int load)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100806{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200807 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaar8e6cbb72020-07-01 14:38:12 +0200808 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100809
Bram Moolenaarb775e722022-11-22 18:12:44 +0000810 // Skip over "s:" before "s:something" to find the import name.
811 int off = name[0] == 's' && name[1] == ':' ? 2 : 0;
812
813 imported_T *ret = find_imported_in_script(name + off, len - off,
814 current_sctx.sc_sid);
Bram Moolenaarb697dc22022-01-22 11:27:29 +0000815 if (ret != NULL && load && (ret->imp_flags & IMP_FLAGS_AUTOLOAD))
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000816 {
Bram Moolenaar753885b2022-08-24 16:30:36 +0100817 scid_T actual_sid = 0;
Bram Moolenaar6809ff92022-07-26 15:10:56 +0100818 int save_emsg_off = emsg_off;
819
820 // "emsg_off" will be set when evaluating an expression silently, but
821 // we do want to know about errors in a script. Also because it then
822 // aborts when an error is encountered.
823 emsg_off = FALSE;
Bram Moolenaar17d36cb2022-01-12 11:46:40 +0000824
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000825 // script found before but not loaded yet
Bram Moolenaarb697dc22022-01-22 11:27:29 +0000826 ret->imp_flags &= ~IMP_FLAGS_AUTOLOAD;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000827 (void)do_source(SCRIPT_ITEM(ret->imp_sid)->sn_name, FALSE,
Bram Moolenaar753885b2022-08-24 16:30:36 +0100828 DOSO_NONE, &actual_sid);
829 // If the script is a symlink it may be sourced with another name, may
830 // need to adjust the script ID for that.
831 if (actual_sid != 0)
832 ret->imp_sid = actual_sid;
Bram Moolenaar6809ff92022-07-26 15:10:56 +0100833
834 emsg_off = save_emsg_off;
Bram Moolenaardc4451d2022-01-09 21:36:37 +0000835 }
836 return ret;
Bram Moolenaarefa94442020-08-08 22:16:00 +0200837}
838
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100839/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000840 * Called when checking for a following operator at "arg". When the rest of
841 * the line is empty or only a comment, peek the next line. If there is a next
842 * line return a pointer to it and set "nextp".
843 * Otherwise skip over white space.
844 */
845 char_u *
846may_peek_next_line(cctx_T *cctx, char_u *arg, char_u **nextp)
847{
848 char_u *p = skipwhite(arg);
849
850 *nextp = NULL;
851 if (*p == NUL || (VIM_ISWHITE(*arg) && vim9_comment_start(p)))
852 {
853 *nextp = peek_next_line_from_context(cctx);
854 if (*nextp != NULL)
855 return *nextp;
856 }
857 return p;
858}
859
860/*
Bram Moolenaar23c55272020-06-21 16:58:13 +0200861 * Return a pointer to the next line that isn't empty or only contains a
862 * comment. Skips over white space.
863 * Returns NULL if there is none.
864 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200865 char_u *
866peek_next_line_from_context(cctx_T *cctx)
Bram Moolenaar23c55272020-06-21 16:58:13 +0200867{
868 int lnum = cctx->ctx_lnum;
869
870 while (++lnum < cctx->ctx_ufunc->uf_lines.ga_len)
871 {
872 char_u *line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[lnum];
Bram Moolenaaracd4c5e2020-06-22 19:39:03 +0200873 char_u *p;
Bram Moolenaar23c55272020-06-21 16:58:13 +0200874
Bram Moolenaarba60cc42020-08-12 19:15:33 +0200875 // ignore NULLs inserted for continuation lines
876 if (line != NULL)
877 {
878 p = skipwhite(line);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100879 if (vim9_bad_comment(p))
880 return NULL;
Bram Moolenaarba60cc42020-08-12 19:15:33 +0200881 if (*p != NUL && !vim9_comment_start(p))
882 return p;
883 }
Bram Moolenaar23c55272020-06-21 16:58:13 +0200884 }
885 return NULL;
886}
887
888/*
Bram Moolenaare6085c52020-04-12 20:19:16 +0200889 * Get the next line of the function from "cctx".
Bram Moolenaar23c55272020-06-21 16:58:13 +0200890 * Skips over empty lines. Skips over comment lines if "skip_comment" is TRUE.
Bram Moolenaare6085c52020-04-12 20:19:16 +0200891 * Returns NULL when at the end.
892 */
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200893 char_u *
Bram Moolenaar23c55272020-06-21 16:58:13 +0200894next_line_from_context(cctx_T *cctx, int skip_comment)
Bram Moolenaare6085c52020-04-12 20:19:16 +0200895{
Bram Moolenaar7a092242020-04-16 22:10:49 +0200896 char_u *line;
Bram Moolenaare6085c52020-04-12 20:19:16 +0200897
898 do
899 {
900 ++cctx->ctx_lnum;
901 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len)
Bram Moolenaar7a092242020-04-16 22:10:49 +0200902 {
903 line = NULL;
Bram Moolenaare6085c52020-04-12 20:19:16 +0200904 break;
Bram Moolenaar7a092242020-04-16 22:10:49 +0200905 }
Bram Moolenaare6085c52020-04-12 20:19:16 +0200906 line = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
Bram Moolenaar7a092242020-04-16 22:10:49 +0200907 cctx->ctx_line_start = line;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200908 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar23c55272020-06-21 16:58:13 +0200909 } while (line == NULL || *skipwhite(line) == NUL
Bram Moolenaar2dd0a2c2020-08-08 15:10:27 +0200910 || (skip_comment && vim9_comment_start(skipwhite(line))));
Bram Moolenaare6085c52020-04-12 20:19:16 +0200911 return line;
912}
913
914/*
Bram Moolenaar5afd0812021-01-03 18:33:13 +0100915 * Skip over white space at "whitep" and assign to "*arg".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200916 * If "*arg" is at the end of the line, advance to the next line.
Bram Moolenaar2c330432020-04-13 14:41:35 +0200917 * Also when "whitep" points to white space and "*arg" is on a "#".
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200918 * Return FAIL if beyond the last line, "*arg" is unmodified then.
919 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000920 int
Bram Moolenaar2c330432020-04-13 14:41:35 +0200921may_get_next_line(char_u *whitep, char_u **arg, cctx_T *cctx)
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200922{
Bram Moolenaar5afd0812021-01-03 18:33:13 +0100923 *arg = skipwhite(whitep);
Bram Moolenaar4b3e1962021-03-18 21:37:55 +0100924 if (vim9_bad_comment(*arg))
925 return FAIL;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +0200926 if (**arg == NUL || (VIM_ISWHITE(*whitep) && vim9_comment_start(*arg)))
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200927 {
Bram Moolenaar23c55272020-06-21 16:58:13 +0200928 char_u *next = next_line_from_context(cctx, TRUE);
Bram Moolenaar9c7e6dd2020-04-12 20:55:20 +0200929
930 if (next == NULL)
931 return FAIL;
932 *arg = skipwhite(next);
933 }
934 return OK;
935}
936
Bram Moolenaara7eedf32020-07-10 21:50:41 +0200937/*
938 * Idem, and give an error when failed.
939 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000940 int
Bram Moolenaara7eedf32020-07-10 21:50:41 +0200941may_get_next_line_error(char_u *whitep, char_u **arg, cctx_T *cctx)
942{
943 if (may_get_next_line(whitep, arg, cctx) == FAIL)
944 {
Bram Moolenaar8ff16e02020-12-07 21:49:52 +0100945 SOURCING_LNUM = cctx->ctx_lnum + 1;
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200946 emsg(_(e_line_incomplete));
Bram Moolenaara7eedf32020-07-10 21:50:41 +0200947 return FAIL;
948 }
949 return OK;
950}
951
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100952/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200953 * Get a line from the compilation context, compatible with exarg_T getline().
954 * Return a pointer to the line in allocated memory.
955 * Return NULL for end-of-file or some error.
956 */
957 static char_u *
958exarg_getline(
959 int c UNUSED,
960 void *cookie,
961 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200962 getline_opt_T options UNUSED)
Bram Moolenaar04b12692020-05-04 23:24:44 +0200963{
964 cctx_T *cctx = (cctx_T *)cookie;
Bram Moolenaar66250c92020-08-20 15:02:42 +0200965 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +0200966
Bram Moolenaar66250c92020-08-20 15:02:42 +0200967 for (;;)
Bram Moolenaar04b12692020-05-04 23:24:44 +0200968 {
Bram Moolenaar2914a202020-09-27 18:24:03 +0200969 if (cctx->ctx_lnum >= cctx->ctx_ufunc->uf_lines.ga_len - 1)
Bram Moolenaar66250c92020-08-20 15:02:42 +0200970 return NULL;
971 ++cctx->ctx_lnum;
972 p = ((char_u **)cctx->ctx_ufunc->uf_lines.ga_data)[cctx->ctx_lnum];
973 // Comment lines result in NULL pointers, skip them.
974 if (p != NULL)
975 return vim_strsave(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +0200976 }
Bram Moolenaar04b12692020-05-04 23:24:44 +0200977}
978
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100979 void
980fill_exarg_from_cctx(exarg_T *eap, cctx_T *cctx)
981{
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100982 eap->ea_getline = exarg_getline;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100983 eap->cookie = cctx;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +0000984 eap->skip = cctx->ctx_skip == SKIP_YES;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100985}
986
Bram Moolenaar04b12692020-05-04 23:24:44 +0200987/*
Bram Moolenaardc7c3662021-12-20 15:04:29 +0000988 * Return TRUE if "ufunc" should be compiled, taking into account whether
989 * "profile" indicates profiling is to be done.
990 */
991 int
992func_needs_compiling(ufunc_T *ufunc, compiletype_T compile_type)
993{
994 switch (ufunc->uf_def_status)
995 {
996 case UF_TO_BE_COMPILED:
997 return TRUE;
998
999 case UF_COMPILED:
1000 {
1001 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1002 + ufunc->uf_dfunc_idx;
1003
1004 switch (compile_type)
1005 {
1006 case CT_PROFILE:
1007#ifdef FEAT_PROFILE
1008 return dfunc->df_instr_prof == NULL;
1009#endif
1010 case CT_NONE:
1011 return dfunc->df_instr == NULL;
1012 case CT_DEBUG:
1013 return dfunc->df_instr_debug == NULL;
1014 }
1015 }
1016
1017 case UF_NOT_COMPILED:
1018 case UF_COMPILE_ERROR:
1019 case UF_COMPILING:
1020 break;
1021 }
1022 return FALSE;
1023}
1024
1025/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02001026 * Compile a nested :def command.
1027 */
1028 static char_u *
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001029compile_nested_function(exarg_T *eap, cctx_T *cctx, garray_T *lines_to_free)
Bram Moolenaar04b12692020-05-04 23:24:44 +02001030{
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001031 int is_global = *eap->arg == 'g' && eap->arg[1] == ':';
Bram Moolenaar04b12692020-05-04 23:24:44 +02001032 char_u *name_start = eap->arg;
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02001033 char_u *name_end = to_name_end(eap->arg, TRUE);
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001034 int off;
1035 char_u *func_name;
Bram Moolenaareef21022020-08-01 22:16:43 +02001036 char_u *lambda_name;
John Marriottb32800f2025-02-01 15:25:34 +01001037 size_t lambda_namelen;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001038 ufunc_T *ufunc;
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001039 int r = FAIL;
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02001040 compiletype_T compile_type;
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001041 int funcref_isn_idx = -1;
Bram Moolenaar1889f492022-08-16 19:34:44 +01001042 lvar_T *lvar = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001043
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001044 if (eap->forceit)
Bram Moolenaar8b848ca2020-09-10 22:28:01 +02001045 {
1046 emsg(_(e_cannot_use_bang_with_nested_def));
1047 return NULL;
1048 }
1049
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001050 if (*name_start == '/')
1051 {
1052 name_end = skip_regexp(name_start + 1, '/', TRUE);
1053 if (*name_end == '/')
1054 ++name_end;
Bram Moolenaar63b91732021-08-05 20:40:03 +02001055 set_nextcmd(eap, name_end);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001056 }
1057 if (name_end == name_start || *skipwhite(name_end) != '(')
1058 {
1059 if (!ends_excmd2(name_start, name_end))
1060 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00001061 if (*skipwhite(name_end) == '.')
1062 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
1063 eap->cmd);
1064 else
1065 semsg(_(e_invalid_command_str), eap->cmd);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01001066 return NULL;
1067 }
1068
1069 // "def" or "def Name": list functions
1070 if (generate_DEF(cctx, name_start, name_end - name_start) == FAIL)
1071 return NULL;
1072 return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
1073 }
1074
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02001075 // Only g:Func() can use a namespace.
1076 if (name_start[1] == ':' && !is_global)
1077 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001078 semsg(_(e_namespace_not_supported_str), name_start);
Bram Moolenaarbcbf4132020-08-01 22:35:13 +02001079 return NULL;
1080 }
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00001081 if (cctx->ctx_skip != SKIP_YES
1082 && check_defined(name_start, name_end - name_start, cctx,
Bram Moolenaardce24412022-02-08 20:35:30 +00001083 NULL, FALSE) == FAIL)
Bram Moolenaareef21022020-08-01 22:16:43 +02001084 return NULL;
Bram Moolenaarf681cfb2022-02-07 20:30:57 +00001085 if (!ASCII_ISUPPER(is_global ? name_start[2] : name_start[0]))
1086 {
Bram Moolenaar3787f262022-02-07 21:54:01 +00001087 semsg(_(e_function_name_must_start_with_capital_str), name_start);
Bram Moolenaarf681cfb2022-02-07 20:30:57 +00001088 return NULL;
1089 }
Bram Moolenaareef21022020-08-01 22:16:43 +02001090
Bram Moolenaar04b12692020-05-04 23:24:44 +02001091 eap->arg = name_end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001092 fill_exarg_from_cctx(eap, cctx);
1093
Bram Moolenaar04b12692020-05-04 23:24:44 +02001094 eap->forceit = FALSE;
Bram Moolenaar38453522021-11-28 22:00:12 +00001095 // We use the special <Lamba>99 name, but it's not really a lambda.
John Marriottb32800f2025-02-01 15:25:34 +01001096 lambda_name = get_lambda_name();
1097 lambda_namelen = get_lambda_name_len();
1098 lambda_name = vim_strnsave(lambda_name, lambda_namelen);
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001099 if (lambda_name == NULL)
1100 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
h-eastb895b0f2023-09-24 15:46:31 +02001115 ufunc = define_function(eap, lambda_name, 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 {
Bram Moolenaar65449bd2022-09-19 16:02:43 +01001151 r = generate_NEWFUNC(cctx, lambda_name, func_name);
Bram Moolenaara915fa02022-03-23 11:29:15 +00001152 func_name = NULL;
1153 lambda_name = NULL;
1154 }
1155 else
1156 {
1157 // Define a local variable for the function reference.
Bram Moolenaar1889f492022-08-16 19:34:44 +01001158 lvar = reserve_local(cctx, func_name, name_end - name_start,
Bram Moolenaar6586a012022-09-30 11:04:50 +01001159 ASSIGN_CONST, ufunc->uf_func_type);
Bram Moolenaara915fa02022-03-23 11:29:15 +00001160 if (lvar == NULL)
1161 goto theend;
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001162 if (generate_FUNCREF(cctx, ufunc, NULL, FALSE, 0, &funcref_isn_idx) == FAIL)
Bram Moolenaara915fa02022-03-23 11:29:15 +00001163 goto theend;
1164 r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
1165 }
1166
Bram Moolenaar139575d2022-03-15 19:29:30 +00001167 compile_type = get_compile_type(ufunc);
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02001168#ifdef FEAT_PROFILE
1169 // If the outer function is profiled, also compile the nested function for
1170 // profiling.
1171 if (cctx->ctx_compile_type == CT_PROFILE)
1172 compile_type = CT_PROFILE;
1173#endif
1174 if (func_needs_compiling(ufunc, compile_type)
1175 && compile_def_function(ufunc, TRUE, compile_type, cctx) == FAIL)
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02001176 {
1177 func_ptr_unref(ufunc);
Bram Moolenaar1889f492022-08-16 19:34:44 +01001178 if (lvar != NULL)
1179 // Now the local variable can't be used.
1180 *lvar->lv_name = '/'; // impossible value
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001181 goto theend;
Bram Moolenaar4ee711f2020-09-23 18:51:11 +02001182 }
Bram Moolenaar04b12692020-05-04 23:24:44 +02001183
Bram Moolenaar648594e2021-07-11 17:55:01 +02001184#ifdef FEAT_PROFILE
1185 // When the outer function is compiled for profiling, the nested function
1186 // may be called without profiling. Compile it here in the right context.
Bram Moolenaarffcfddc2021-07-11 20:22:30 +02001187 if (compile_type == CT_PROFILE && func_needs_compiling(ufunc, CT_NONE))
Bram Moolenaar648594e2021-07-11 17:55:01 +02001188 compile_def_function(ufunc, FALSE, CT_NONE, cctx);
1189#endif
1190
Bram Moolenaara915fa02022-03-23 11:29:15 +00001191 // If a FUNCREF instruction was generated, set the index after compiling.
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001192 if (funcref_isn_idx != -1 && ufunc->uf_def_status == UF_COMPILED)
1193 {
1194 isn_T *funcref_isn = ((isn_T *)cctx->ctx_instr.ga_data) +
1195 funcref_isn_idx;
Bram Moolenaara915fa02022-03-23 11:29:15 +00001196 funcref_isn->isn_arg.funcref.fr_dfunc_idx = ufunc->uf_dfunc_idx;
Yegappan Lakshmanana76fbe62023-09-27 18:51:43 +02001197 }
Bram Moolenaar58a52f22020-12-22 18:56:55 +01001198
1199theend:
1200 vim_free(lambda_name);
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001201 vim_free(func_name);
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001202 return r == FAIL ? NULL : (char_u *)"";
Bram Moolenaar04b12692020-05-04 23:24:44 +02001203}
1204
1205/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001206 * Compile one Vim expression {expr} in string "p".
1207 * "p" points to the opening "{".
1208 * Return a pointer to the character after "}", NULL for an error.
1209 */
1210 char_u *
1211compile_one_expr_in_str(char_u *p, cctx_T *cctx)
1212{
1213 char_u *block_start;
1214 char_u *block_end;
1215
1216 // Skip the opening {.
1217 block_start = skipwhite(p + 1);
1218 block_end = block_start;
1219 if (*block_start != NUL && skip_expr(&block_end, NULL) == FAIL)
1220 return NULL;
1221 block_end = skipwhite(block_end);
1222 // The block must be closed by a }.
1223 if (*block_end != '}')
1224 {
1225 semsg(_(e_missing_close_curly_str), p);
1226 return NULL;
1227 }
1228 if (compile_expr0(&block_start, cctx) == FAIL)
1229 return NULL;
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02001230 may_generate_2STRING(-1, TOSTRING_INTERPOLATE, cctx);
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001231
1232 return block_end + 1;
1233}
1234
1235/*
LemonBoy2eaef102022-05-06 13:14:50 +01001236 * Compile a string "str" (either containing a literal string or a mix of
1237 * literal strings and Vim expressions of the form `{expr}`). This is used
1238 * when compiling a heredoc assignment to a variable or an interpolated string
1239 * in a Vim9 def function. Vim9 instructions are generated to push strings,
1240 * evaluate expressions, concatenate them and create a list of lines. When
1241 * "evalstr" is TRUE, Vim expressions in "str" are evaluated.
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001242 */
1243 int
LemonBoy2eaef102022-05-06 13:14:50 +01001244compile_all_expr_in_str(char_u *str, int evalstr, cctx_T *cctx)
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001245{
LemonBoy2eaef102022-05-06 13:14:50 +01001246 char_u *p = str;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001247 char_u *val;
LemonBoy2eaef102022-05-06 13:14:50 +01001248 int count = 0;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001249
1250 if (cctx->ctx_skip == SKIP_YES)
1251 return OK;
1252
LemonBoy2eaef102022-05-06 13:14:50 +01001253 if (!evalstr || *str == NUL)
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001254 {
LemonBoy2eaef102022-05-06 13:14:50 +01001255 // Literal string, possibly empty.
1256 val = *str != NUL ? vim_strsave(str) : NULL;
1257 return generate_PUSHS(cctx, &val);
1258 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001259
LemonBoy2eaef102022-05-06 13:14:50 +01001260 // Push all the string pieces to the stack, followed by a ISN_CONCAT.
1261 while (*p != NUL)
1262 {
1263 char_u *lit_start;
LemonBoy2eaef102022-05-06 13:14:50 +01001264 int escaped_brace = FALSE;
1265
1266 // Look for a block start.
1267 lit_start = p;
1268 while (*p != '{' && *p != '}' && *p != NUL)
1269 ++p;
1270
1271 if (*p != NUL && *p == p[1])
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001272 {
LemonBoy2eaef102022-05-06 13:14:50 +01001273 // Escaped brace, unescape and continue.
1274 // Include the brace in the literal string.
1275 ++p;
1276 escaped_brace = TRUE;
1277 }
1278 else if (*p == '}')
1279 {
1280 semsg(_(e_stray_closing_curly_str), str);
1281 return FAIL;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001282 }
LemonBoy372bcce2022-04-25 12:43:20 +01001283
LemonBoy2eaef102022-05-06 13:14:50 +01001284 // Append the literal part.
1285 if (p != lit_start)
1286 {
1287 val = vim_strnsave(lit_start, (size_t)(p - lit_start));
1288 if (generate_PUSHS(cctx, &val) == FAIL)
1289 return FAIL;
1290 ++count;
1291 }
1292
1293 if (*p == NUL)
1294 break;
1295
1296 if (escaped_brace)
1297 {
1298 // Skip the second brace.
1299 ++p;
1300 continue;
1301 }
1302
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001303 p = compile_one_expr_in_str(p, cctx);
1304 if (p == NULL)
LemonBoy2eaef102022-05-06 13:14:50 +01001305 return FAIL;
LemonBoy2eaef102022-05-06 13:14:50 +01001306 ++count;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001307 }
LemonBoy2eaef102022-05-06 13:14:50 +01001308
1309 // Small optimization, if there's only a single piece skip the ISN_CONCAT.
Bram Moolenaar0abc2872022-05-10 13:24:30 +01001310 if (count > 1)
LemonBoy2eaef102022-05-06 13:14:50 +01001311 return generate_CONCAT(cctx, count);
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +01001312
1313 return OK;
1314}
1315
1316/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001317 * Return the length of an assignment operator, or zero if there isn't one.
1318 */
1319 int
1320assignment_len(char_u *p, int *heredoc)
1321{
1322 if (*p == '=')
1323 {
1324 if (p[1] == '<' && p[2] == '<')
1325 {
1326 *heredoc = TRUE;
1327 return 3;
1328 }
1329 return 1;
1330 }
1331 if (vim_strchr((char_u *)"+-*/%", *p) != NULL && p[1] == '=')
1332 return 2;
1333 if (STRNCMP(p, "..=", 3) == 0)
1334 return 3;
1335 return 0;
1336}
1337
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001338/*
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001339 * Generate the load instruction for "name".
1340 */
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001341 static int
Bram Moolenaard505d172022-12-18 21:42:55 +00001342generate_loadvar(cctx_T *cctx, lhs_T *lhs)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001343{
Bram Moolenaard505d172022-12-18 21:42:55 +00001344 char_u *name = lhs->lhs_name;
1345 type_T *type = lhs->lhs_type;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001346 int res = OK;
Bram Moolenaard505d172022-12-18 21:42:55 +00001347
1348 switch (lhs->lhs_dest)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001349 {
1350 case dest_option:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001351 case dest_func_option:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001352 generate_LOAD(cctx, ISN_LOADOPT, 0, name, type);
1353 break;
1354 case dest_global:
Bram Moolenaar03290b82020-12-19 16:30:44 +01001355 if (vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001356 {
1357 if (name[2] == NUL)
1358 generate_instr_type(cctx, ISN_LOADGDICT, &t_dict_any);
1359 else
1360 generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
1361 }
Bram Moolenaar03290b82020-12-19 16:30:44 +01001362 else
1363 generate_LOAD(cctx, ISN_LOADAUTO, 0, name, type);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001364 break;
1365 case dest_buffer:
1366 generate_LOAD(cctx, ISN_LOADB, 0, name + 2, type);
1367 break;
1368 case dest_window:
1369 generate_LOAD(cctx, ISN_LOADW, 0, name + 2, type);
1370 break;
1371 case dest_tab:
1372 generate_LOAD(cctx, ISN_LOADT, 0, name + 2, type);
1373 break;
1374 case dest_script:
Ernie Rael3f821d62024-04-24 20:07:50 +02001375 case dest_script_v9:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001376 res = compile_load_scriptvar(cctx,
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +01001377 name + (name[1] == ':' ? 2 : 0), NULL, NULL);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001378 break;
1379 case dest_env:
1380 // Include $ in the name here
1381 generate_LOAD(cctx, ISN_LOADENV, 0, name, type);
1382 break;
1383 case dest_reg:
1384 generate_LOAD(cctx, ISN_LOADREG, name[1], NULL, &t_string);
1385 break;
1386 case dest_vimvar:
Bram Moolenaarc3caa7f2022-05-25 19:15:10 +01001387 generate_LOADV(cctx, name + 2);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001388 break;
1389 case dest_local:
Bram Moolenaar4875d6a2022-08-17 15:55:51 +01001390 if (cctx->ctx_skip != SKIP_YES)
1391 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001392 lvar_T *lvar = lhs->lhs_lvar;
Bram Moolenaar4875d6a2022-08-17 15:55:51 +01001393 if (lvar->lv_from_outer > 0)
1394 generate_LOADOUTER(cctx, lvar->lv_idx, lvar->lv_from_outer,
Bram Moolenaar65449bd2022-09-19 16:02:43 +01001395 lvar->lv_loop_depth, lvar->lv_loop_idx, type);
Bram Moolenaar4875d6a2022-08-17 15:55:51 +01001396 else
1397 generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
1398 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001399 break;
Bram Moolenaard505d172022-12-18 21:42:55 +00001400 case dest_class_member:
1401 generate_CLASSMEMBER(cctx, TRUE, lhs->lhs_class,
1402 lhs->lhs_classmember_idx);
1403 break;
Bram Moolenaardc234ca2020-11-28 18:52:33 +01001404 case dest_expr:
1405 // list or dict value should already be on the stack.
1406 break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001407 }
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001408
1409 return res;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001410}
1411
Bram Moolenaardc234ca2020-11-28 18:52:33 +01001412/*
1413 * Skip over "[expr]" or ".member".
1414 * Does not check for any errors.
1415 */
1416 static char_u *
1417skip_index(char_u *start)
1418{
1419 char_u *p = start;
1420
1421 if (*p == '[')
1422 {
1423 p = skipwhite(p + 1);
1424 (void)skip_expr(&p, NULL);
1425 p = skipwhite(p);
1426 if (*p == ']')
1427 return p + 1;
1428 return p;
1429 }
1430 // if (*p == '.')
1431 return to_name_end(p + 1, TRUE);
1432}
1433
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001434 void
1435vim9_declare_error(char_u *name)
1436{
1437 char *scope = "";
1438
1439 switch (*name)
1440 {
Bram Moolenaar7fe87552020-06-21 20:38:28 +02001441 case 'g': scope = _("global"); break;
1442 case 'b': scope = _("buffer"); break;
1443 case 'w': scope = _("window"); break;
1444 case 't': scope = _("tab"); break;
1445 case 'v': scope = "v:"; break;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001446 case '$': semsg(_(e_cannot_declare_an_environment_variable_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001447 return;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001448 case '&': semsg(_(e_cannot_declare_an_option_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001449 return;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001450 case '@': semsg(_(e_cannot_declare_a_register_str), name);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001451 return;
Bram Moolenaar7fe87552020-06-21 20:38:28 +02001452 default: return;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001453 }
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001454 semsg(_(e_cannot_declare_a_scope_variable_str), scope, name);
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001455}
1456
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001457/*
Bram Moolenaar3558afe2022-10-13 16:12:57 +01001458 * Return TRUE if "name" is a valid register to use.
1459 * Return FALSE and give an error message if not.
1460 */
1461 static int
1462valid_dest_reg(int name)
1463{
1464 if ((name == '@' || valid_yank_reg(name, FALSE)) && name != '.')
1465 return TRUE;
1466 emsg_invreg(name);
1467 return FAIL;
1468}
1469
1470/*
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001471 * For one assignment figure out the type of destination. Return it in "dest".
1472 * When not recognized "dest" is not set.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001473 * For an option "option_scope" is set.
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001474 * For a v:var "vimvaridx" is set.
1475 * "type" is set to the destination type if known, unchanted otherwise.
1476 * Return FAIL if an error message was given.
1477 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00001478 int
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001479get_var_dest(
1480 char_u *name,
1481 assign_dest_T *dest,
Bram Moolenaar97f8c102022-04-02 19:43:57 +01001482 cmdidx_T cmdidx,
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001483 int *option_scope,
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001484 int *vimvaridx,
1485 type_T **type,
1486 cctx_T *cctx)
1487{
1488 char_u *p;
1489
1490 if (*name == '&')
1491 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001492 int cc;
1493 long numval;
1494 getoption_T opt_type;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001495 int opt_p_flags;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001496
1497 *dest = dest_option;
1498 if (cmdidx == CMD_final || cmdidx == CMD_const)
1499 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001500 emsg(_(e_cannot_lock_option));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001501 return FAIL;
1502 }
1503 p = name;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001504 p = find_option_end(&p, option_scope);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001505 if (p == NULL)
1506 {
1507 // cannot happen?
Bram Moolenaar108010a2021-06-27 22:03:33 +02001508 emsg(_(e_unexpected_characters_in_assignment));
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001509 return FAIL;
1510 }
1511 cc = *p;
1512 *p = NUL;
1513 opt_type = get_option_value(skip_option_env_lead(name),
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001514 &numval, NULL, &opt_p_flags, *option_scope);
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001515 *p = cc;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001516 switch (opt_type)
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001517 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001518 case gov_unknown:
Bram Moolenaare1242042021-12-16 20:56:57 +00001519 semsg(_(e_unknown_option_str), name);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001520 return FAIL;
1521 case gov_string:
1522 case gov_hidden_string:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00001523 if (opt_p_flags & P_FUNC)
1524 {
1525 // might be a Funcref, check the type later
1526 *type = &t_any;
1527 *dest = dest_func_option;
1528 }
1529 else
1530 {
1531 *type = &t_string;
1532 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001533 break;
1534 case gov_bool:
1535 case gov_hidden_bool:
1536 *type = &t_bool;
1537 break;
1538 case gov_number:
1539 case gov_hidden_number:
1540 *type = &t_number;
1541 break;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001542 }
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001543 }
1544 else if (*name == '$')
1545 {
1546 *dest = dest_env;
1547 *type = &t_string;
1548 }
1549 else if (*name == '@')
1550 {
Bram Moolenaar3558afe2022-10-13 16:12:57 +01001551 if (!valid_dest_reg(name[1]))
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001552 return FAIL;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001553 *dest = dest_reg;
Bram Moolenaar74f4a962021-06-17 21:03:07 +02001554 *type = name[1] == '#' ? &t_number_or_string : &t_string;
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01001555 }
1556 else if (STRNCMP(name, "g:", 2) == 0)
1557 {
1558 *dest = dest_global;
1559 }
1560 else if (STRNCMP(name, "b:", 2) == 0)
1561 {
1562 *dest = dest_buffer;
1563 }
1564 else if (STRNCMP(name, "w:", 2) == 0)
1565 {
1566 *dest = dest_window;
1567 }
1568 else if (STRNCMP(name, "t:", 2) == 0)
1569 {
1570 *dest = dest_tab;
1571 }
1572 else if (STRNCMP(name, "v:", 2) == 0)
1573 {
1574 typval_T *vtv;
1575 int di_flags;
1576
1577 *vimvaridx = find_vim_var(name + 2, &di_flags);
1578 if (*vimvaridx < 0)
1579 {
1580 semsg(_(e_variable_not_found_str), name);
1581 return FAIL;
1582 }
1583 // We use the current value of "sandbox" here, is that OK?
1584 if (var_check_ro(di_flags, name, FALSE))
1585 return FAIL;
1586 *dest = dest_vimvar;
1587 vtv = get_vim_var_tv(*vimvaridx);
1588 *type = typval2type_vimvar(vtv, cctx->ctx_type_list);
1589 }
1590 return OK;
1591}
1592
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001593 static int
Bram Moolenaar97f8c102022-04-02 19:43:57 +01001594is_decl_command(cmdidx_T cmdidx)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001595{
1596 return cmdidx == CMD_let || cmdidx == CMD_var
1597 || cmdidx == CMD_final || cmdidx == CMD_const;
1598}
1599
1600/*
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02001601 * Returns TRUE if the class or object variable in "lhs" is modifiable.
1602 * "var_start" points to the start of the variable name and "lhs->lhs_varlen"
1603 * has the total length. Note that the "lhs" can be nested an object reference
1604 * (e.g. a.b.c.d.var).
1605 */
1606 static int
1607lhs_class_member_modifiable(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
1608{
1609 size_t varlen = lhs->lhs_varlen;
1610 class_T *cl = lhs->lhs_type->tt_class;
1611 int is_object = lhs->lhs_type->tt_type == VAR_OBJECT;
1612 char_u *name = var_start + varlen + 1;
1613 size_t namelen = lhs->lhs_end - var_start - varlen - 1;
1614 ocmember_T *m;
1615
1616 m = member_lookup(cl, lhs->lhs_type->tt_type, name, namelen, NULL);
1617 if (m == NULL)
1618 {
1619 member_not_found_msg(cl, lhs->lhs_type->tt_type, name, namelen);
1620 return FALSE;
1621 }
1622
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01001623 if (IS_ENUM(cl))
1624 {
1625 semsg(_(e_enumvalue_str_cannot_be_modified), cl->class_name,
1626 m->ocm_name);
1627 return FALSE;
1628 }
1629
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02001630 // If it is private member variable, then accessing it outside the
1631 // class is not allowed.
1632 // If it is a read only class variable, then it can be modified
1633 // only inside the class where it is defined.
1634 if ((m->ocm_access != VIM_ACCESS_ALL) &&
1635 ((is_object && !inside_class(cctx, cl))
1636 || (!is_object && cctx->ctx_ufunc->uf_class != cl)))
1637 {
1638 char *msg = (m->ocm_access == VIM_ACCESS_PRIVATE)
Ernie Rael03042a22023-11-11 08:53:32 +01001639 ? e_cannot_access_protected_variable_str
RestorerZ7fe8f432023-09-24 23:21:24 +02001640 : e_variable_is_not_writable_str;
Ernie Raele6c9aa52023-10-06 19:55:52 +02001641 emsg_var_cl_define(msg, m->ocm_name, 0, cl);
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02001642 return FALSE;
1643 }
1644
1645 return TRUE;
1646}
1647
1648/*
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001649 * Initialize "lhs" with default values
Bram Moolenaar752fc692021-01-04 21:57:11 +01001650 */
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001651 static void
1652lhs_init_defaults(lhs_T *lhs)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001653{
Bram Moolenaar752fc692021-01-04 21:57:11 +01001654 CLEAR_POINTER(lhs);
1655 lhs->lhs_dest = dest_local;
1656 lhs->lhs_vimvaridx = -1;
1657 lhs->lhs_scriptvar_idx = -1;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001658 lhs->lhs_member_idx = -1;
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001659}
Bram Moolenaar752fc692021-01-04 21:57:11 +01001660
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001661/*
1662 * When compiling a LHS variable name, find the end of the destination and the
1663 * end of the variable name.
1664 */
1665 static int
1666lhs_find_var_end(
1667 lhs_T *lhs,
1668 char_u *var_start,
1669 int is_decl,
1670 char_u **var_endp)
1671{
1672 char_u *var_end = *var_endp;
1673
1674 // "lhs_dest_end" is the end of the destination, including "[expr]" or
Bram Moolenaar752fc692021-01-04 21:57:11 +01001675 // ".name".
1676 // "var_end" is the end of the variable/option/etc. name.
1677 lhs->lhs_dest_end = skip_var_one(var_start, FALSE);
1678 if (*var_start == '@')
Bram Moolenaar3558afe2022-10-13 16:12:57 +01001679 {
1680 if (!valid_dest_reg(var_start[1]))
1681 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001682 var_end = var_start + 2;
Bram Moolenaar3558afe2022-10-13 16:12:57 +01001683 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01001684 else
1685 {
1686 // skip over the leading "&", "&l:", "&g:" and "$"
1687 var_end = skip_option_env_lead(var_start);
1688 var_end = to_name_end(var_end, TRUE);
1689 }
1690
1691 // "a: type" is declaring variable "a" with a type, not dict "a:".
1692 if (is_decl && lhs->lhs_dest_end == var_start + 2
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001693 && lhs->lhs_dest_end[-1] == ':')
Bram Moolenaar752fc692021-01-04 21:57:11 +01001694 --lhs->lhs_dest_end;
1695 if (is_decl && var_end == var_start + 2 && var_end[-1] == ':')
1696 --var_end;
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001697
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00001698 lhs->lhs_end = lhs->lhs_dest_end;
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001699 *var_endp = var_end;
1700
1701 return OK;
1702}
1703
1704/*
1705 * Set various fields in "lhs"
1706 */
1707 static int
1708lhs_init(
1709 lhs_T *lhs,
1710 char_u *var_start,
1711 int is_decl,
1712 int heredoc,
1713 char_u **var_endp)
1714{
1715 char_u *var_end = *var_endp;
1716
1717 lhs_init_defaults(lhs);
1718
1719 // Find the end of the variable and the destination
1720 if (lhs_find_var_end(lhs, var_start, is_decl, &var_end) == FAIL)
1721 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001722
1723 // compute the length of the destination without "[expr]" or ".name"
1724 lhs->lhs_varlen = var_end - var_start;
Bram Moolenaar753bcf82021-04-21 14:24:24 +02001725 lhs->lhs_varlen_total = lhs->lhs_varlen;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001726 lhs->lhs_name = vim_strnsave(var_start, lhs->lhs_varlen);
1727 if (lhs->lhs_name == NULL)
1728 return FAIL;
Bram Moolenaar08251752021-01-11 21:20:18 +01001729
1730 if (lhs->lhs_dest_end > var_start + lhs->lhs_varlen)
1731 // Something follows after the variable: "var[idx]" or "var.key".
1732 lhs->lhs_has_index = TRUE;
1733
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001734 lhs->lhs_type = heredoc ? &t_list_string : &t_any;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001735
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001736 *var_endp = var_end;
1737
1738 return OK;
1739}
1740
1741/*
1742 * Compile a LHS class variable name.
1743 */
1744 static int
1745compile_lhs_class_variable(
1746 cctx_T *cctx,
1747 lhs_T *lhs,
1748 class_T *defcl,
1749 int is_decl)
1750{
1751 if (cctx->ctx_ufunc->uf_defclass != defcl)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001752 {
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001753 // A class variable can be accessed without the class name
1754 // only inside a class.
1755 semsg(_(e_class_variable_str_accessible_only_inside_class_str),
1756 lhs->lhs_name, defcl->class_name);
1757 return FAIL;
1758 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01001759
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001760 if (is_decl)
1761 {
1762 semsg(_(e_variable_already_declared_in_class_str), lhs->lhs_name);
1763 return FAIL;
1764 }
1765
1766 ocmember_T *m = &defcl->class_class_members[lhs->lhs_classmember_idx];
1767 if (oc_var_check_ro(defcl, m))
1768 return FAIL;
1769
1770 lhs->lhs_dest = dest_class_member;
1771 // The class variable is defined either in the current class or
1772 // in one of the parent class in the hierarchy.
1773 lhs->lhs_class = defcl;
1774 lhs->lhs_type = oc_member_type_by_idx(defcl, FALSE,
1775 lhs->lhs_classmember_idx);
1776
1777 return OK;
1778}
1779
1780/*
1781 * Compile an imported LHS variable
1782 */
1783 static int
1784compile_lhs_import_var(
1785 lhs_T *lhs,
1786 imported_T *import,
1787 char_u *var_start,
1788 char_u **var_endp,
1789 char_u **rawnamep)
1790{
1791 char_u *var_end = *var_endp;
1792 char_u *dot = vim_strchr(var_start, '.');
1793 char_u *p;
1794
1795 // for an import the name is what comes after the dot
1796 if (dot == NULL)
1797 {
1798 semsg(_(e_no_dot_after_imported_name_str), var_start);
1799 return FAIL;
1800 }
1801
1802 p = skipwhite(dot + 1);
1803 var_end = to_name_end(p, TRUE);
1804 if (var_end == p)
1805 {
1806 semsg(_(e_missing_name_after_imported_name_str), var_start);
1807 return FAIL;
1808 }
1809
1810 vim_free(lhs->lhs_name);
1811 lhs->lhs_varlen = var_end - p;
1812 lhs->lhs_name = vim_strnsave(p, lhs->lhs_varlen);
1813 if (lhs->lhs_name == NULL)
1814 return FAIL;
1815 *rawnamep = lhs->lhs_name;
1816 lhs->lhs_scriptvar_sid = import->imp_sid;
1817
1818 // TODO: where do we check this name is exported?
1819
1820 // Check if something follows: "exp.var[idx]" or
1821 // "exp.var.key".
1822 lhs->lhs_has_index = lhs->lhs_dest_end > skipwhite(var_end);
1823
1824 *var_endp = var_end;
1825
1826 return OK;
1827}
1828
1829/*
1830 * Process a script-local variable when compiling a LHS variable name.
1831 */
1832 static int
1833compile_lhs_script_var(
1834 cctx_T *cctx,
1835 lhs_T *lhs,
1836 char_u *var_start,
1837 char_u *var_end,
1838 int is_decl)
1839{
1840 int script_namespace = FALSE;
1841 int script_var = FALSE;
1842 imported_T *import;
1843 char_u *var_name;
Yegappan Lakshmanan084529c2024-12-24 09:50:01 +01001844 size_t var_name_len;
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001845
1846 if (lhs->lhs_varlen > 1 && STRNCMP(var_start, "s:", 2) == 0)
1847 script_namespace = TRUE;
1848
1849 if (script_namespace)
1850 {
1851 var_name = var_start + 2;
1852 var_name_len = lhs->lhs_varlen - 2;
1853 }
1854 else
1855 {
1856 var_name = var_start;
1857 var_name_len = lhs->lhs_varlen;
1858 }
1859
1860 if (script_var_exists(var_name, var_name_len, cctx, NULL) == OK)
1861 script_var = TRUE;
1862
1863 import = find_imported(var_start, lhs->lhs_varlen, FALSE);
1864
1865 if (script_namespace || script_var || import != NULL)
1866 {
1867 char_u *rawname = lhs->lhs_name + (lhs->lhs_name[1] == ':' ? 2 : 0);
1868
1869 if (script_namespace && current_script_is_vim9())
Bram Moolenaar752fc692021-01-04 21:57:11 +01001870 {
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001871 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), var_start);
1872 return FAIL;
1873 }
1874
1875 if (is_decl)
1876 {
1877 if (script_namespace)
1878 semsg(_(e_cannot_declare_script_variable_in_function_str),
1879 lhs->lhs_name);
1880 else
1881 semsg(_(e_variable_already_declared_in_script_str),
1882 lhs->lhs_name);
1883 return FAIL;
1884 }
1885 else if (cctx->ctx_ufunc->uf_script_ctx_version == SCRIPT_VERSION_VIM9
1886 && script_namespace
1887 && !script_var && import == NULL)
1888 {
1889 semsg(_(e_unknown_variable_str), lhs->lhs_name);
1890 return FAIL;
1891 }
1892
1893 lhs->lhs_dest = current_script_is_vim9() ? dest_script_v9 :
1894 dest_script;
1895
1896 // existing script-local variables should have a type
1897 lhs->lhs_scriptvar_sid = current_sctx.sc_sid;
1898 if (import != NULL)
1899 {
1900 if (compile_lhs_import_var(lhs, import, var_start, &var_end,
1901 &rawname) == FAIL)
1902 return FAIL;
1903 }
1904
1905 if (SCRIPT_ID_VALID(lhs->lhs_scriptvar_sid))
1906 {
1907 // Check writable only when no index follows.
1908 lhs->lhs_scriptvar_idx = get_script_item_idx(
1909 lhs->lhs_scriptvar_sid, rawname,
1910 lhs->lhs_has_index ? ASSIGN_FINAL :
1911 ASSIGN_CONST, cctx, NULL);
1912 if (lhs->lhs_scriptvar_idx >= 0)
1913 {
1914 scriptitem_T *si = SCRIPT_ITEM(lhs->lhs_scriptvar_sid);
1915 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1916 + lhs->lhs_scriptvar_idx;
1917
1918 lhs->lhs_type = sv->sv_type;
1919 }
1920 }
1921
1922 return OK;
1923 }
1924
1925 return check_defined(var_start, lhs->lhs_varlen, cctx, NULL, FALSE);
1926}
1927
1928/*
1929 * Compile the LHS destination.
1930 */
1931 static int
1932compile_lhs_var_dest(
1933 cctx_T *cctx,
1934 lhs_T *lhs,
1935 int cmdidx,
1936 char_u *var_start,
1937 char_u *var_end,
1938 int is_decl)
1939{
1940 int declare_error = FALSE;
1941
1942 if (get_var_dest(lhs->lhs_name, &lhs->lhs_dest, cmdidx,
1943 &lhs->lhs_opt_flags, &lhs->lhs_vimvaridx,
1944 &lhs->lhs_type, cctx) == FAIL)
1945 return FAIL;
1946
1947 if (lhs->lhs_dest != dest_local && cmdidx != CMD_const
1948 && cmdidx != CMD_final)
1949 {
1950 // Specific kind of variable recognized.
1951 declare_error = is_decl;
1952 }
1953 else
1954 {
1955 class_T *defcl;
1956
1957 // No specific kind of variable recognized, just a name.
1958 if (check_reserved_name(lhs->lhs_name, lhs->lhs_has_index
1959 && *var_end == '.') == FAIL)
1960 return FAIL;
1961
1962 if (lookup_local(var_start, lhs->lhs_varlen, &lhs->lhs_local_lvar,
1963 cctx) == OK)
1964 {
1965 lhs->lhs_lvar = &lhs->lhs_local_lvar;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001966 }
1967 else
1968 {
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001969 CLEAR_FIELD(lhs->lhs_arg_lvar);
1970 if (arg_exists(var_start, lhs->lhs_varlen,
1971 &lhs->lhs_arg_lvar.lv_idx, &lhs->lhs_arg_lvar.lv_type,
1972 &lhs->lhs_arg_lvar.lv_from_outer, cctx) == OK)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001973 {
1974 if (is_decl)
1975 {
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001976 semsg(_(e_str_is_used_as_argument), lhs->lhs_name);
Bram Moolenaar752fc692021-01-04 21:57:11 +01001977 return FAIL;
1978 }
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001979 lhs->lhs_lvar = &lhs->lhs_arg_lvar;
Bram Moolenaar752fc692021-01-04 21:57:11 +01001980 }
1981 }
1982
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001983 if (lhs->lhs_lvar != NULL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01001984 {
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01001985 if (is_decl)
1986 {
1987 // if we come here with what looks like an assignment like
1988 // .= but which has been rejected by assignment_len() from
1989 // may_compile_assignment give a better error message
1990 char_u *p = skipwhite(lhs->lhs_end);
1991 if (p[0] == '.' && p[1] == '=')
1992 emsg(_(e_dot_equal_not_supported_with_script_version_two));
1993 else if (p[0] == ':')
1994 // type specified in a non-var assignment
1995 semsg(_(e_trailing_characters_str), p);
1996 else
1997 semsg(_(e_variable_already_declared_str), lhs->lhs_name);
1998 return FAIL;
1999 }
2000 }
2001 else if ((lhs->lhs_classmember_idx = cctx_class_member_idx(
2002 cctx, var_start, lhs->lhs_varlen, &defcl)) >= 0)
2003 {
2004 if (compile_lhs_class_variable(cctx, lhs, defcl, is_decl)
2005 == FAIL)
2006 return FAIL;
2007 }
2008 else
2009 {
2010 if (compile_lhs_script_var(cctx, lhs, var_start, var_end,
2011 is_decl) == FAIL)
2012 return FAIL;
2013 }
2014 }
2015
2016 if (declare_error)
2017 {
2018 vim9_declare_error(lhs->lhs_name);
2019 return FAIL;
2020 }
2021
2022 return OK;
2023}
2024
2025/*
2026 * When compiling a LHS variable name, for a class or an object, set the LHS
2027 * member type.
2028 */
2029 static int
2030compile_lhs_set_oc_member_type(
2031 cctx_T *cctx,
2032 lhs_T *lhs,
2033 char_u *var_start)
2034{
2035 class_T *cl = lhs->lhs_type->tt_class;
2036 int is_object = lhs->lhs_type->tt_type == VAR_OBJECT;
2037 char_u *name = var_start + lhs->lhs_varlen + 1;
2038 size_t namelen = lhs->lhs_end - var_start - lhs->lhs_varlen - 1;
2039
2040 ocmember_T *m = member_lookup(cl, lhs->lhs_type->tt_type,
2041 name, namelen, &lhs->lhs_member_idx);
2042 if (m == NULL)
2043 {
2044 member_not_found_msg(cl, lhs->lhs_type->tt_type, name, namelen);
2045 return FAIL;
2046 }
2047
2048 if (IS_ENUM(cl))
2049 {
2050 if (!inside_class(cctx, cl))
2051 {
2052 semsg(_(e_enumvalue_str_cannot_be_modified),
2053 cl->class_name, m->ocm_name);
Bram Moolenaar752fc692021-01-04 21:57:11 +01002054 return FAIL;
2055 }
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002056 if (lhs->lhs_type->tt_type == VAR_OBJECT &&
2057 lhs->lhs_member_idx < 2)
2058 {
2059 char *msg = lhs->lhs_member_idx == 0 ?
2060 e_enum_str_name_cannot_be_modified :
2061 e_enum_str_ordinal_cannot_be_modified;
2062 semsg(_(msg), cl->class_name);
2063 return FAIL;
2064 }
2065 }
2066
2067 // If it is private member variable, then accessing it outside the
2068 // class is not allowed.
2069 // If it is a read only class variable, then it can be modified
2070 // only inside the class where it is defined.
2071 if ((m->ocm_access != VIM_ACCESS_ALL) &&
2072 ((is_object && !inside_class(cctx, cl))
2073 || (!is_object && cctx->ctx_ufunc->uf_class != cl)))
2074 {
2075 char *msg = (m->ocm_access == VIM_ACCESS_PRIVATE)
2076 ? e_cannot_access_protected_variable_str
2077 : e_variable_is_not_writable_str;
2078 emsg_var_cl_define(msg, m->ocm_name, 0, cl);
2079 return FAIL;
2080 }
2081
2082 if (!IS_CONSTRUCTOR_METHOD(cctx->ctx_ufunc)
2083 && oc_var_check_ro(cl, m))
2084 return FAIL;
2085
2086 lhs->lhs_member_type = m->ocm_type;
2087
2088 return OK;
2089}
2090
2091/*
2092 * When compiling a LHS variable, set the LHS variable type.
2093 */
2094 static int
2095compile_lhs_set_type(cctx_T *cctx, lhs_T *lhs, char_u *var_end, int is_decl)
2096{
2097 if (is_decl && *skipwhite(var_end) == ':')
2098 {
2099 char_u *p;
2100
2101 // parse optional type: "let var: type = expr"
2102 if (VIM_ISWHITE(*var_end))
2103 {
2104 semsg(_(e_no_white_space_allowed_before_colon_str), var_end);
2105 return FAIL;
2106 }
2107
2108 if (!VIM_ISWHITE(var_end[1]))
2109 {
2110 semsg(_(e_white_space_required_after_str_str), ":", var_end);
2111 return FAIL;
2112 }
2113
2114 p = skipwhite(var_end + 1);
2115 lhs->lhs_type = parse_type(&p, cctx->ctx_type_list, TRUE);
2116 if (lhs->lhs_type == NULL)
2117 return FAIL;
2118
2119 lhs->lhs_has_type = TRUE;
2120 lhs->lhs_end = p;
2121 }
2122 else if (lhs->lhs_lvar != NULL)
2123 lhs->lhs_type = lhs->lhs_lvar->lv_type;
2124
2125 return OK;
2126}
2127
2128/*
2129 * Returns TRUE if "lhs" is a concatenable string.
2130 */
2131 static int
2132lhs_concatenable(lhs_T *lhs)
2133{
2134 return lhs->lhs_dest == dest_global
2135 || lhs->lhs_has_index
2136 || lhs->lhs_type->tt_type == VAR_STRING
2137 || lhs->lhs_type->tt_type == VAR_ANY;
2138}
2139
2140/*
2141 * Create a new local variable when compiling a LHS variable.
2142 */
2143 static int
2144compile_lhs_new_local_var(
2145 cctx_T *cctx,
2146 lhs_T *lhs,
2147 char_u *var_start,
2148 int cmdidx,
2149 int oplen,
2150 int is_decl,
2151 int has_cmd,
2152 int heredoc)
2153{
2154 if (oplen > 1 && !heredoc)
2155 {
2156 // +=, /=, etc. require an existing variable
2157 semsg(_(e_cannot_use_operator_on_new_variable_str), lhs->lhs_name);
2158 return FAIL;
2159 }
2160
2161 if (!is_decl || (lhs->lhs_has_index && !has_cmd
2162 && cctx->ctx_skip != SKIP_YES))
2163 {
2164 semsg(_(e_unknown_variable_str), lhs->lhs_name);
2165 return FAIL;
2166 }
2167
2168 // Check the name is valid for a funcref.
2169 if (lhs->lhs_type->tt_type == VAR_FUNC
2170 || lhs->lhs_type->tt_type == VAR_PARTIAL)
2171 {
2172 if (var_wrong_func_name(lhs->lhs_name, TRUE))
2173 return FAIL;
2174 }
2175
2176 // New local variable.
2177 int assign;
2178 switch (cmdidx)
2179 {
2180 case CMD_final:
2181 assign = ASSIGN_FINAL; break;
2182 case CMD_const:
2183 assign = ASSIGN_CONST; break;
2184 default:
2185 assign = ASSIGN_VAR; break;
2186 }
2187
2188 lhs->lhs_lvar = reserve_local(cctx, var_start, lhs->lhs_varlen, assign,
2189 lhs->lhs_type);
2190 if (lhs->lhs_lvar == NULL)
2191 return FAIL;
2192
2193 lhs->lhs_new_local = TRUE;
2194
2195 return OK;
2196}
2197
2198/*
2199 * When compiling a LHS variable name, set the LHS member type.
2200 */
2201 static int
2202compile_lhs_set_member_type(
2203 cctx_T *cctx,
2204 lhs_T *lhs,
2205 char_u *var_start,
2206 int is_decl,
2207 int has_cmd)
2208{
2209 lhs->lhs_member_type = lhs->lhs_type;
2210
2211 if (!lhs->lhs_has_index)
2212 return OK;
2213
2214 char_u *after = var_start + lhs->lhs_varlen;
2215 char_u *p;
2216
2217 // Something follows after the variable: "var[idx]" or "var.key".
2218 if (is_decl && cctx->ctx_skip != SKIP_YES)
2219 {
2220 if (has_cmd)
2221 emsg(_(e_cannot_use_index_when_declaring_variable));
2222 else
2223 semsg(_(e_unknown_variable_str), lhs->lhs_name);
2224 return FAIL;
2225 }
2226
2227 // Now: var_start[lhs->lhs_varlen] is '[' or '.'
2228 // Only the last index is used below, if there are others
2229 // before it generate code for the expression. Thus for
2230 // "ll[1][2]" the expression is "ll[1]" and "[2]" is the index.
2231 for (;;)
2232 {
2233 p = skip_index(after);
2234 if (*p != '[' && *p != '.')
2235 {
2236 lhs->lhs_varlen_total = p - var_start;
2237 break;
2238 }
2239 after = p;
2240 }
2241 if (after > var_start + lhs->lhs_varlen)
2242 {
2243 lhs->lhs_varlen = after - var_start;
2244 lhs->lhs_dest = dest_expr;
2245 // We don't know the type before evaluating the expression,
2246 // use "any" until then.
2247 lhs->lhs_type = &t_any;
2248 }
2249
2250 int use_class = lhs->lhs_type != NULL
2251 && (lhs->lhs_type->tt_type == VAR_CLASS
2252 || lhs->lhs_type->tt_type == VAR_OBJECT);
2253
2254 if (lhs->lhs_type == NULL
2255 || (use_class ? lhs->lhs_type->tt_class == NULL
2256 : lhs->lhs_type->tt_member == NULL))
2257 {
2258 lhs->lhs_member_type = &t_any;
2259 }
2260 else if (use_class)
2261 {
2262 // for an object or class member get the type of the member
2263 if (compile_lhs_set_oc_member_type(cctx, lhs, var_start) == FAIL)
2264 return FAIL;
2265 }
2266 else
2267 lhs->lhs_member_type = lhs->lhs_type->tt_member;
2268
2269 return OK;
2270}
2271
2272/*
2273 * Figure out the LHS type and other properties for an assignment or one item
2274 * of ":unlet" with an index.
2275 * Returns OK or FAIL.
2276 */
2277 int
2278compile_lhs(
2279 char_u *var_start,
2280 lhs_T *lhs,
2281 cmdidx_T cmdidx,
2282 int heredoc,
2283 int has_cmd, // "var" before "var_start"
2284 int oplen,
2285 cctx_T *cctx)
2286{
Yegappan Lakshmanan59c88802024-12-19 20:00:31 +01002287 char_u *var_end = NULL;
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002288 int is_decl = is_decl_command(cmdidx);
2289
2290 if (lhs_init(lhs, var_start, is_decl, heredoc, &var_end) == FAIL)
2291 return FAIL;
2292
2293 if (cctx->ctx_skip != SKIP_YES)
2294 {
2295 // compile the LHS destination
2296 if (compile_lhs_var_dest(cctx, lhs, cmdidx, var_start, var_end,
2297 is_decl) == FAIL)
2298 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002299 }
2300
Bram Moolenaarab36e6a2021-11-30 16:14:49 +00002301 // handle "a:name" as a name, not index "name" in "a"
Bram Moolenaar752fc692021-01-04 21:57:11 +01002302 if (lhs->lhs_varlen > 1 || var_start[lhs->lhs_varlen] != ':')
2303 var_end = lhs->lhs_dest_end;
2304
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002305 if (lhs->lhs_dest != dest_option && lhs->lhs_dest != dest_func_option)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002306 {
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002307 // set the LHS variable type
2308 if (compile_lhs_set_type(cctx, lhs, var_end, is_decl) == FAIL)
2309 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002310 }
2311
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002312 if (oplen == 3 && !heredoc && !lhs_concatenable(lhs))
Bram Moolenaar752fc692021-01-04 21:57:11 +01002313 {
2314 emsg(_(e_can_only_concatenate_to_string));
2315 return FAIL;
2316 }
2317
2318 if (lhs->lhs_lvar == NULL && lhs->lhs_dest == dest_local
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002319 && cctx->ctx_skip != SKIP_YES)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002320 {
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002321 if (compile_lhs_new_local_var(cctx, lhs, var_start, cmdidx, oplen,
2322 is_decl, has_cmd, heredoc) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002323 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002324 }
2325
Yegappan Lakshmanand0186c52024-12-18 20:16:20 +01002326 if (compile_lhs_set_member_type(cctx, lhs, var_start, is_decl, has_cmd)
2327 == FAIL)
2328 return FAIL;
Bram Moolenaarc750d912021-11-29 22:02:12 +00002329
Bram Moolenaar752fc692021-01-04 21:57:11 +01002330 return OK;
2331}
2332
2333/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002334 * Figure out the LHS and check a few errors.
2335 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002336 int
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002337compile_assign_lhs(
Bram Moolenaar97f8c102022-04-02 19:43:57 +01002338 char_u *var_start,
2339 lhs_T *lhs,
2340 cmdidx_T cmdidx,
2341 int is_decl,
2342 int heredoc,
2343 int has_cmd, // "var" before "var_start"
2344 int oplen,
2345 cctx_T *cctx)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002346{
Bram Moolenaar97f8c102022-04-02 19:43:57 +01002347 if (compile_lhs(var_start, lhs, cmdidx, heredoc, has_cmd, oplen, cctx)
2348 == FAIL)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002349 return FAIL;
2350
2351 if (!lhs->lhs_has_index && lhs->lhs_lvar == &lhs->lhs_arg_lvar)
2352 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002353 semsg(_(e_cannot_assign_to_argument_str), lhs->lhs_name);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002354 return FAIL;
2355 }
2356 if (!is_decl && lhs->lhs_lvar != NULL
Bram Moolenaar6586a012022-09-30 11:04:50 +01002357 && lhs->lhs_lvar->lv_const != ASSIGN_VAR
2358 && !lhs->lhs_has_index)
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002359 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002360 semsg(_(e_cannot_assign_to_constant_str), lhs->lhs_name);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002361 return FAIL;
2362 }
2363 return OK;
2364}
2365
2366/*
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002367 * Return TRUE if "lhs" has a range index: "[expr : expr]".
2368 */
2369 static int
2370has_list_index(char_u *idx_start, cctx_T *cctx)
2371{
2372 char_u *p = idx_start;
2373 int save_skip;
2374
2375 if (*p != '[')
2376 return FALSE;
2377
2378 p = skipwhite(p + 1);
2379 if (*p == ':')
2380 return TRUE;
2381
2382 save_skip = cctx->ctx_skip;
2383 cctx->ctx_skip = SKIP_YES;
2384 (void)compile_expr0(&p, cctx);
2385 cctx->ctx_skip = save_skip;
2386 return *skipwhite(p) == ':';
2387}
2388
2389/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002390 * For an assignment with an index, compile the "idx" in "var[idx]" or "key" in
2391 * "var.key".
Bram Moolenaar752fc692021-01-04 21:57:11 +01002392 */
2393 static int
Bram Moolenaare42939a2021-04-05 17:11:17 +02002394compile_assign_index(
Bram Moolenaar752fc692021-01-04 21:57:11 +01002395 char_u *var_start,
2396 lhs_T *lhs,
Bram Moolenaare42939a2021-04-05 17:11:17 +02002397 int *range,
Bram Moolenaar752fc692021-01-04 21:57:11 +01002398 cctx_T *cctx)
2399{
Bram Moolenaar752fc692021-01-04 21:57:11 +01002400 size_t varlen = lhs->lhs_varlen;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002401 char_u *p;
2402 int r = OK;
Bram Moolenaar68452172021-04-12 21:21:02 +02002403 int need_white_before = TRUE;
2404 int empty_second;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002405
Bram Moolenaar752fc692021-01-04 21:57:11 +01002406 p = var_start + varlen;
2407 if (*p == '[')
2408 {
2409 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02002410 if (*p == ':')
2411 {
2412 // empty first index, push zero
2413 r = generate_PUSHNR(cctx, 0);
2414 need_white_before = FALSE;
2415 }
2416 else
2417 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002418
2419 if (r == OK && *skipwhite(p) == ':')
2420 {
2421 // unlet var[idx : idx]
Bram Moolenaar68452172021-04-12 21:21:02 +02002422 // blob[idx : idx] = value
Bram Moolenaare42939a2021-04-05 17:11:17 +02002423 *range = TRUE;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002424 p = skipwhite(p);
Bram Moolenaar68452172021-04-12 21:21:02 +02002425 empty_second = *skipwhite(p + 1) == ']';
2426 if ((need_white_before && !IS_WHITE_OR_NUL(p[-1]))
2427 || (!empty_second && !IS_WHITE_OR_NUL(p[1])))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002428 {
2429 semsg(_(e_white_space_required_before_and_after_str_at_str),
2430 ":", p);
2431 return FAIL;
2432 }
2433 p = skipwhite(p + 1);
Bram Moolenaar68452172021-04-12 21:21:02 +02002434 if (*p == ']')
2435 // empty second index, push "none"
2436 r = generate_PUSHSPEC(cctx, VVAL_NONE);
2437 else
2438 r = compile_expr0(&p, cctx);
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002439 }
2440
Bram Moolenaar752fc692021-01-04 21:57:11 +01002441 if (r == OK && *skipwhite(p) != ']')
2442 {
2443 // this should not happen
Bram Moolenaare1242042021-12-16 20:56:57 +00002444 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar752fc692021-01-04 21:57:11 +01002445 r = FAIL;
2446 }
2447 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002448 else if (lhs->lhs_member_idx >= 0)
2449 {
2450 // object member index
2451 r = generate_PUSHNR(cctx, lhs->lhs_member_idx);
2452 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002453 else // if (*p == '.')
2454 {
2455 char_u *key_end = to_name_end(p + 1, TRUE);
2456 char_u *key = vim_strnsave(p + 1, key_end - p - 1);
2457
Zdenek Dohnal9fe17d42021-08-04 22:30:52 +02002458 r = generate_PUSHS(cctx, &key);
Bram Moolenaar752fc692021-01-04 21:57:11 +01002459 }
Bram Moolenaare42939a2021-04-05 17:11:17 +02002460 return r;
2461}
2462
2463/*
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002464 * For a LHS with an index, load the variable to be indexed.
2465 */
2466 static int
2467compile_load_lhs(
2468 lhs_T *lhs,
2469 char_u *var_start,
2470 type_T *rhs_type,
2471 cctx_T *cctx)
2472{
2473 if (lhs->lhs_dest == dest_expr)
2474 {
2475 size_t varlen = lhs->lhs_varlen;
2476 int c = var_start[varlen];
Bram Moolenaare97976b2021-08-01 13:17:17 +02002477 int lines_len = cctx->ctx_ufunc->uf_lines.ga_len;
Bram Moolenaare97976b2021-08-01 13:17:17 +02002478 int res;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002479
Bram Moolenaare97976b2021-08-01 13:17:17 +02002480 // Evaluate "ll[expr]" of "ll[expr][idx]". End the line with a NUL and
2481 // limit the lines array length to avoid skipping to a following line.
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002482 var_start[varlen] = NUL;
Bram Moolenaare97976b2021-08-01 13:17:17 +02002483 cctx->ctx_ufunc->uf_lines.ga_len = cctx->ctx_lnum + 1;
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002484 char_u *p = var_start;
Bram Moolenaare97976b2021-08-01 13:17:17 +02002485 res = compile_expr0(&p, cctx);
2486 var_start[varlen] = c;
2487 cctx->ctx_ufunc->uf_lines.ga_len = lines_len;
2488 if (res == FAIL || p != var_start + varlen)
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002489 {
2490 // this should not happen
Bram Moolenaare97976b2021-08-01 13:17:17 +02002491 if (res != FAIL)
Bram Moolenaare1242042021-12-16 20:56:57 +00002492 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002493 return FAIL;
2494 }
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002495
Bram Moolenaar078a4612022-01-04 15:17:03 +00002496 lhs->lhs_type = cctx->ctx_type_stack.ga_len == 0 ? &t_void
2497 : get_type_on_stack(cctx, 0);
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02002498
Yegappan Lakshmanan54d7f182025-02-10 21:35:07 +01002499 if (lhs->lhs_type->tt_type == VAR_CLASS
2500 || lhs->lhs_type->tt_type == VAR_OBJECT)
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02002501 {
Yegappan Lakshmanan54d7f182025-02-10 21:35:07 +01002502 // Check whether the class or object variable is modifiable
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02002503 if (!lhs_class_member_modifiable(lhs, var_start, cctx))
2504 return FAIL;
2505 }
2506
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002507 // Now we can properly check the type. The variable is indexed, thus
2508 // we need the member type. For a class or object we don't know the
2509 // type yet, it depends on what member is used.
Yegappan Lakshmananc229a6a2023-10-26 23:05:07 +02002510 // The top item in the stack is the Dict, followed by the key and then
2511 // the type of the value.
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002512 vartype_T vartype = lhs->lhs_type->tt_type;
2513 type_T *member_type = lhs->lhs_type->tt_member;
2514 if (rhs_type != NULL && member_type != NULL
2515 && vartype != VAR_OBJECT && vartype != VAR_CLASS
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002516 && rhs_type != &t_void
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002517 && need_type(rhs_type, member_type, FALSE,
Yegappan Lakshmananc229a6a2023-10-26 23:05:07 +02002518 -3, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002519 return FAIL;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002520
2521 return OK;
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002522 }
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002523
2524 return generate_loadvar(cctx, lhs);
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002525}
2526
2527/*
Bram Moolenaara369c3d2021-04-21 16:00:10 +02002528 * Produce code for loading "lhs" and also take care of an index.
2529 * Return OK/FAIL.
2530 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002531 int
Bram Moolenaara369c3d2021-04-21 16:00:10 +02002532compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
2533{
Bram Moolenaar4cae8452023-01-15 15:51:48 +00002534 if (lhs->lhs_type->tt_type == VAR_OBJECT)
2535 {
Bram Moolenaar22363c62023-04-24 17:15:25 +01002536 // "this.value": load "this" object and get the value at index for an
2537 // object or class member get the type of the member.
2538 // Also for "obj.value".
Yegappan Lakshmananff6f0d52023-12-21 16:46:18 +01002539 char_u *dot = vim_strchr(var_start, '.');
2540 if (dot == NULL)
2541 {
2542 semsg(_(e_missing_dot_after_object_str), lhs->lhs_name);
2543 return FAIL;
2544 }
Bram Moolenaar22363c62023-04-24 17:15:25 +01002545
Yegappan Lakshmananeb91e242023-08-31 18:10:46 +02002546 class_T *cl = lhs->lhs_type->tt_class;
Yegappan Lakshmanan1ea42882023-10-11 21:43:52 +02002547 type_T *type = oc_member_type(cl, TRUE, dot + 1,
Yegappan Lakshmanan00cd1822023-09-18 19:56:49 +02002548 lhs->lhs_end, &lhs->lhs_member_idx);
Bram Moolenaar4cae8452023-01-15 15:51:48 +00002549 if (lhs->lhs_member_idx < 0)
2550 return FAIL;
2551
Bram Moolenaar22363c62023-04-24 17:15:25 +01002552 if (dot - var_start == 4 && STRNCMP(var_start, "this", 4) == 0)
2553 {
2554 // load "this"
Yegappan Lakshmanand990bf02024-03-22 19:56:17 +01002555 lvar_T *lvar = lhs->lhs_lvar;
2556 int rc;
2557
2558 if (lvar->lv_from_outer > 0)
2559 rc = generate_LOADOUTER(cctx, lvar->lv_idx,
2560 lvar->lv_from_outer, lvar->lv_loop_depth,
2561 lvar->lv_loop_idx, type);
2562 else
2563 rc = generate_LOAD(cctx, ISN_LOAD, lvar->lv_idx, NULL, type);
2564
2565 if (rc == FAIL)
Bram Moolenaar22363c62023-04-24 17:15:25 +01002566 return FAIL;
2567 }
2568 else
2569 {
2570 // load object variable or argument
2571 if (compile_load_lhs(lhs, var_start, lhs->lhs_type, cctx) == FAIL)
2572 return FAIL;
2573 }
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01002574 if (IS_INTERFACE(cl))
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02002575 return generate_GET_ITF_MEMBER(cctx, cl, lhs->lhs_member_idx, type);
2576 return generate_GET_OBJ_MEMBER(cctx, lhs->lhs_member_idx, type);
Bram Moolenaar4cae8452023-01-15 15:51:48 +00002577 }
Yegappan Lakshmanan1ea42882023-10-11 21:43:52 +02002578 else if (lhs->lhs_type->tt_type == VAR_CLASS)
2579 {
2580 // "<classname>.value": load class variable "classname.value"
Yegappan Lakshmananff6f0d52023-12-21 16:46:18 +01002581 char_u *dot = vim_strchr(var_start, '.');
2582 if (dot == NULL)
2583 {
2584 check_type_is_value(lhs->lhs_type);
2585 return FAIL;
2586 }
Yegappan Lakshmanan1ea42882023-10-11 21:43:52 +02002587
2588 class_T *cl = lhs->lhs_type->tt_class;
2589 ocmember_T *m = class_member_lookup(cl, dot + 1,
2590 lhs->lhs_end - dot - 1,
2591 &lhs->lhs_member_idx);
2592 if (m == NULL)
2593 return FAIL;
2594
2595 return generate_CLASSMEMBER(cctx, TRUE, cl, lhs->lhs_member_idx);
2596 }
Bram Moolenaar4cae8452023-01-15 15:51:48 +00002597
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002598 if (compile_load_lhs(lhs, var_start, NULL, cctx) == FAIL)
2599 return FAIL;
Bram Moolenaara369c3d2021-04-21 16:00:10 +02002600
2601 if (lhs->lhs_has_index)
2602 {
2603 int range = FALSE;
2604
2605 // Get member from list or dict. First compile the
2606 // index value.
2607 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
2608 return FAIL;
2609 if (range)
2610 {
2611 semsg(_(e_cannot_use_range_with_assignment_operator_str),
2612 var_start);
2613 return FAIL;
2614 }
2615
2616 // Get the member.
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002617 if (compile_member(FALSE, NULL, cctx) == FAIL)
Bram Moolenaara369c3d2021-04-21 16:00:10 +02002618 return FAIL;
2619 }
2620 return OK;
2621}
2622
2623/*
Bram Moolenaare42939a2021-04-05 17:11:17 +02002624 * Assignment to a list or dict member, or ":unlet" for the item, using the
2625 * information in "lhs".
2626 * Returns OK or FAIL.
2627 */
Bram Moolenaardc7c3662021-12-20 15:04:29 +00002628 int
Bram Moolenaare42939a2021-04-05 17:11:17 +02002629compile_assign_unlet(
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002630 char_u *var_start,
2631 lhs_T *lhs,
2632 int is_assign,
2633 type_T *rhs_type,
2634 cctx_T *cctx)
Bram Moolenaare42939a2021-04-05 17:11:17 +02002635{
Bram Moolenaare42939a2021-04-05 17:11:17 +02002636 vartype_T dest_type;
Bram Moolenaare42939a2021-04-05 17:11:17 +02002637 int range = FALSE;
2638
Bram Moolenaar68452172021-04-12 21:21:02 +02002639 if (compile_assign_index(var_start, lhs, &range, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002640 return FAIL;
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002641 if (is_assign && range
2642 && lhs->lhs_type->tt_type != VAR_LIST
2643 && lhs->lhs_type != &t_blob
2644 && lhs->lhs_type != &t_any)
Bram Moolenaar68452172021-04-12 21:21:02 +02002645 {
2646 semsg(_(e_cannot_use_range_with_assignment_str), var_start);
2647 return FAIL;
2648 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002649
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002650 if (lhs->lhs_type == NULL || lhs->lhs_type == &t_any)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002651 {
2652 // Index on variable of unknown type: check at runtime.
2653 dest_type = VAR_ANY;
2654 }
2655 else
2656 {
2657 dest_type = lhs->lhs_type->tt_type;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002658 if (dest_type == VAR_DICT && range)
2659 {
zeertzjq276410e2023-05-07 21:59:33 +01002660 emsg(_(e_cannot_use_range_with_dictionary));
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002661 return FAIL;
2662 }
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002663 if (dest_type == VAR_DICT
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +02002664 && may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002665 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02002666 if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002667 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002668 type_T *type;
2669
2670 if (range)
2671 {
Bram Moolenaar078a4612022-01-04 15:17:03 +00002672 type = get_type_on_stack(cctx, 1);
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002673 if (need_type(type, &t_number, FALSE,
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002674 -2, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002675 return FAIL;
Bram Moolenaar51e93322021-04-17 20:44:56 +02002676 }
Bram Moolenaar078a4612022-01-04 15:17:03 +00002677 type = get_type_on_stack(cctx, 0);
Bram Moolenaar53ba6ca2022-03-10 19:23:28 +00002678 if ((dest_type != VAR_BLOB && type->tt_type != VAR_SPECIAL)
Bram Moolenaarc6951a72022-12-29 20:56:24 +00002679 && need_type(type, &t_number, FALSE,
Bram Moolenaar51e93322021-04-17 20:44:56 +02002680 -1, 0, cctx, FALSE, FALSE) == FAIL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002681 return FAIL;
2682 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002683 }
2684
Bram Moolenaar4875d6a2022-08-17 15:55:51 +01002685 if (cctx->ctx_skip == SKIP_YES)
2686 return OK;
2687
Bram Moolenaar22363c62023-04-24 17:15:25 +01002688 // Load the dict, list or object. On the stack we then have:
Bram Moolenaar752fc692021-01-04 21:57:11 +01002689 // - value (for assignment, not for :unlet)
2690 // - index
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002691 // - for [a : b] second index
Bram Moolenaar752fc692021-01-04 21:57:11 +01002692 // - variable
Bram Moolenaarb9c0cd82021-04-05 20:51:00 +02002693 if (compile_load_lhs(lhs, var_start, rhs_type, cctx) == FAIL)
2694 return FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002695
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002696 if (dest_type == VAR_LIST
2697 || dest_type == VAR_DICT
2698 || dest_type == VAR_BLOB
2699 || dest_type == VAR_CLASS
2700 || dest_type == VAR_OBJECT
2701 || dest_type == VAR_ANY)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002702 {
2703 if (is_assign)
2704 {
Bram Moolenaar68452172021-04-12 21:21:02 +02002705 if (range)
2706 {
2707 if (generate_instr_drop(cctx, ISN_STORERANGE, 4) == NULL)
2708 return FAIL;
2709 }
2710 else
2711 {
2712 isn_T *isn = generate_instr_drop(cctx, ISN_STOREINDEX, 3);
Bram Moolenaar752fc692021-01-04 21:57:11 +01002713
Bram Moolenaar68452172021-04-12 21:21:02 +02002714 if (isn == NULL)
2715 return FAIL;
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002716 isn->isn_arg.storeindex.si_vartype = dest_type;
2717 isn->isn_arg.storeindex.si_class = NULL;
2718
2719 if (dest_type == VAR_OBJECT)
2720 {
Bram Moolenaarb1e32ac2023-02-21 12:38:51 +00002721 class_T *cl = lhs->lhs_type->tt_class;
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002722
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +01002723 if (IS_INTERFACE(cl))
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002724 {
2725 // "this.value": load "this" object and get the value
2726 // at index for an object or class member get the type
2727 // of the member
2728 isn->isn_arg.storeindex.si_class = cl;
2729 ++cl->class_refcount;
2730 }
2731 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002732 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002733 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002734 else if (range)
2735 {
2736 if (generate_instr_drop(cctx, ISN_UNLETRANGE, 3) == NULL)
2737 return FAIL;
2738 }
Bram Moolenaar752fc692021-01-04 21:57:11 +01002739 else
2740 {
2741 if (generate_instr_drop(cctx, ISN_UNLETINDEX, 2) == NULL)
2742 return FAIL;
2743 }
2744 }
2745 else
2746 {
2747 emsg(_(e_indexable_type_required));
2748 return FAIL;
2749 }
2750
2751 return OK;
2752}
2753
Bram Moolenaar4b8a0652020-12-01 16:30:44 +01002754/*
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002755 * Generate an instruction to push the default value for "vartype".
2756 * if "dest_local" is TRUE then for some types no instruction is generated.
2757 * "skip_store" is set to TRUE if no PUSH instruction is generated.
2758 * Returns OK or FAIL.
2759 */
2760 static int
2761push_default_value(
2762 cctx_T *cctx,
2763 vartype_T vartype,
2764 int dest_is_local,
2765 int *skip_store)
2766{
2767 int r = OK;
2768
2769 switch (vartype)
2770 {
2771 case VAR_BOOL:
2772 r = generate_PUSHBOOL(cctx, VVAL_FALSE);
2773 break;
2774 case VAR_FLOAT:
2775 r = generate_PUSHF(cctx, 0.0);
2776 break;
2777 case VAR_STRING:
2778 r = generate_PUSHS(cctx, NULL);
2779 break;
2780 case VAR_BLOB:
2781 r = generate_PUSHBLOB(cctx, blob_alloc());
2782 break;
2783 case VAR_FUNC:
2784 r = generate_PUSHFUNC(cctx, NULL, &t_func_void, TRUE);
2785 break;
2786 case VAR_LIST:
2787 r = generate_NEWLIST(cctx, 0, FALSE);
2788 break;
2789 case VAR_DICT:
2790 r = generate_NEWDICT(cctx, 0, FALSE);
2791 break;
2792 case VAR_JOB:
2793 r = generate_PUSHJOB(cctx);
2794 break;
2795 case VAR_CHANNEL:
2796 r = generate_PUSHCHANNEL(cctx);
2797 break;
Ernie Rael5c018be2023-08-27 18:40:26 +02002798 case VAR_OBJECT:
2799 r = generate_PUSHOBJ(cctx);
2800 break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002801 case VAR_NUMBER:
2802 case VAR_UNKNOWN:
2803 case VAR_ANY:
2804 case VAR_PARTIAL:
2805 case VAR_VOID:
2806 case VAR_INSTR:
2807 case VAR_CLASS:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002808 case VAR_TYPEALIAS:
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00002809 case VAR_SPECIAL: // cannot happen
2810 // This is skipped for local variables, they are always
2811 // initialized to zero. But in a "for" or "while" loop
2812 // the value may have been changed.
2813 if (dest_is_local && !inside_loop_scope(cctx))
2814 *skip_store = TRUE;
2815 else
2816 r = generate_PUSHNR(cctx, 0);
2817 break;
2818 }
2819 return r;
2820}
2821
2822/*
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002823 * Compile assignment context. Used when compiling an assignment statement.
2824 */
2825typedef struct cac_S cac_T;
2826struct cac_S
2827{
2828 cmdidx_T cac_cmdidx; // assignment command
2829 char_u *cac_nextc; // next character to parse
2830 lhs_T cac_lhs; // lhs of the assignment
2831 type_T *cac_rhs_type; // rhs type of an assignment
2832 char_u *cac_op; // assignment operator
2833 int cac_oplen; // assignment operator length
2834 char_u *cac_var_start; // start of the variable names
2835 char_u *cac_var_end; // end of the variable names
2836 int cac_var_count; // number of variables in assignment
2837 int cac_var_idx; // variable index in a list
2838 int cac_semicolon; // semicolon in [var1, var2; var3]
2839 garray_T *cac_instr;
2840 int cac_instr_count;
2841 int cac_incdec;
2842 int cac_did_generate_slice;
2843 int cac_is_decl;
2844 int cac_is_const;
2845 int cac_start_lnum;
2846 type_T *cac_inferred_type;
2847 int cac_skip_store;
2848};
2849
2850/*
2851 * Initialize the compile assignment context.
2852 */
2853 static void
2854compile_assign_context_init(cac_T *cac, cctx_T *cctx, int cmdidx, char_u *arg)
2855{
2856 CLEAR_FIELD(*cac);
2857 cac->cac_cmdidx = cmdidx;
2858 cac->cac_instr = &cctx->ctx_instr;
2859 cac->cac_rhs_type = &t_any;
2860 cac->cac_is_decl = is_decl_command(cmdidx);
2861 cac->cac_start_lnum = SOURCING_LNUM;
2862 cac->cac_instr_count = -1;
2863 cac->cac_var_end = arg;
2864}
2865
2866/*
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01002867 * Compile an object member variable assignment in the arguments passed to a
2868 * class new() method.
2869 *
2870 * Instruction format:
2871 *
2872 * ifargisset <n> this.<varname> = <value>
2873 *
2874 * where <n> is the index of the default argument.
2875 *
2876 * Generates the ISN_JUMP_IF_ARG_NOT_SET instruction to skip the assignment if
2877 * the value is passed as an argument to the new() method call.
2878 *
2879 * Returns OK on success.
2880 */
2881 static int
2882compile_assign_obj_new_arg(char_u **argp, cctx_T *cctx)
2883{
2884 char_u *arg = *argp;
2885
2886 arg += 11; // skip "ifargisset"
2887 int def_arg_idx = getdigits(&arg);
2888 arg = skipwhite(arg);
2889
2890 // Use a JUMP_IF_ARG_NOT_SET instruction to skip if the value was not
2891 // given and the default value is "v:none".
2892 int stack_offset = STACK_FRAME_SIZE +
2893 (cctx->ctx_ufunc->uf_va_name != NULL ? 1 : 0);
2894 int def_arg_count = cctx->ctx_ufunc->uf_def_args.ga_len;
2895 int arg_offset = def_arg_idx - def_arg_count - stack_offset;
2896
2897 if (generate_JUMP_IF_ARG(cctx, ISN_JUMP_IF_ARG_NOT_SET,
2898 arg_offset) == FAIL)
2899 return FAIL;
2900
2901 *argp = arg;
2902 return OK;
2903}
2904
2905/*
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002906 * Translate the increment (++) and decrement (--) operators to the
2907 * corresponding compound operators (+= or -=).
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01002908 *
2909 * Returns OK on success and FAIL on syntax error.
2910 */
2911 static int
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002912translate_incdec_op(exarg_T *eap, cac_T *cac)
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01002913{
2914 if (VIM_ISWHITE(eap->cmd[2]))
2915 {
2916 semsg(_(e_no_white_space_allowed_after_str_str),
2917 eap->cmdidx == CMD_increment ? "++" : "--", eap->cmd);
2918 return FAIL;
2919 }
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002920 cac->cac_op = (char_u *)(eap->cmdidx == CMD_increment ? "+=" : "-=");
2921 cac->cac_oplen = 2;
2922 cac->cac_incdec = TRUE;
2923
2924 return OK;
2925}
2926
2927/*
2928 * Process the operator in an assignment statement.
2929 */
2930 static int
2931compile_assign_process_operator(
2932 exarg_T *eap,
2933 char_u *arg,
2934 cac_T *cac,
2935 int *heredoc,
2936 char_u **retstr)
2937{
2938 *retstr = NULL;
2939
2940 if (eap->cmdidx == CMD_increment || eap->cmdidx == CMD_decrement)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002941 // Change an unary operator to a compound operator
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01002942 return translate_incdec_op(eap, cac);
2943
2944 char_u *sp = cac->cac_nextc;
2945 cac->cac_nextc = skipwhite(cac->cac_nextc);
2946 cac->cac_op = cac->cac_nextc;
2947 cac->cac_oplen = assignment_len(cac->cac_nextc, heredoc);
2948
2949 if (cac->cac_var_count > 0 && cac->cac_oplen == 0)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002950 {
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01002951 // can be something like "[1, 2]->func()"
2952 *retstr = arg;
2953 return FAIL;
2954 }
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002955
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01002956 // need white space before and after the operator
2957 if (cac->cac_oplen > 0 && (!VIM_ISWHITE(*sp)
2958 || !IS_WHITE_OR_NUL(cac->cac_op[cac->cac_oplen])))
2959 {
2960 error_white_both(cac->cac_op, cac->cac_oplen);
2961 return FAIL;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002962 }
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01002963
2964 return OK;
2965}
2966
2967/*
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01002968 * Find the start of an assignment statement.
2969 */
2970 static char_u *
2971compile_assign_compute_start(char_u *arg, int var_count)
2972{
2973 if (var_count > 0)
2974 // [var1, var2] = [val1, val2]
2975 // skip over the "["
2976 return skipwhite(arg + 1);
2977
2978 return arg;
2979}
2980
2981/*
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01002982 * Parse a heredoc assignment starting at "p". Returns a pointer to the
2983 * beginning of the heredoc content.
2984 */
2985 static char_u *
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002986parse_heredoc_assignment(exarg_T *eap, cctx_T *cctx, cac_T *cac)
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01002987{
2988 // [let] varname =<< [trim] {end}
2989 eap->ea_getline = exarg_getline;
2990 eap->cookie = cctx;
2991
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002992 list_T *l = heredoc_get(eap, cac->cac_nextc + 3, FALSE, TRUE);
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01002993 if (l == NULL)
2994 return NULL;
2995
2996 list_free(l);
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002997 cac->cac_nextc += STRLEN(cac->cac_nextc);
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01002998
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01002999 return cac->cac_nextc;
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003000}
3001
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003002/*
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003003 * Check the type of a RHS expression in a list assignment statement.
3004 * The RHS expression is already compiled. So the type is on the stack.
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003005 */
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003006 static int
3007compile_assign_list_check_rhs_type(cctx_T *cctx, cac_T *cac)
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003008{
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003009 type_T *stacktype;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003010
3011 stacktype = cctx->ctx_type_stack.ga_len == 0 ? &t_void
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003012 : get_type_on_stack(cctx, 0);
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003013 if (stacktype->tt_type == VAR_VOID)
3014 {
3015 emsg(_(e_cannot_use_void_value));
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003016 return FAIL;
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003017 }
3018
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003019 if (need_type(stacktype, &t_list_any, FALSE, -1, 0, cctx,
3020 FALSE, FALSE) == FAIL)
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003021 return FAIL;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003022
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003023 if (stacktype->tt_member != NULL)
3024 cac->cac_rhs_type = stacktype->tt_member;
3025
3026 return OK;
3027}
3028
3029/*
3030 * In a list assignment statement, if a constant list was used, check the
3031 * length. Returns OK if the length check succeeds. Returns FAIL otherwise.
3032 */
3033 static int
3034compile_assign_list_check_length(cctx_T *cctx, cac_T *cac)
3035{
3036 int needed_list_len;
3037 int did_check = FALSE;
3038
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003039 needed_list_len = cac->cac_semicolon
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003040 ? cac->cac_var_count - 1
3041 : cac->cac_var_count;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003042 if (cac->cac_instr->ga_len > 0)
3043 {
3044 isn_T *isn = ((isn_T *)cac->cac_instr->ga_data) +
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003045 cac->cac_instr->ga_len - 1;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003046
3047 if (isn->isn_type == ISN_NEWLIST)
3048 {
3049 did_check = TRUE;
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003050 if (cac->cac_semicolon ?
3051 isn->isn_arg.number < needed_list_len
3052 : isn->isn_arg.number != needed_list_len)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003053 {
3054 semsg(_(e_expected_nr_items_but_got_nr),
3055 needed_list_len, (int)isn->isn_arg.number);
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003056 return FAIL;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003057 }
3058 }
3059 }
3060
3061 if (!did_check)
3062 generate_CHECKLEN(cctx, needed_list_len, cac->cac_semicolon);
3063
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003064 return OK;
3065}
3066
3067/*
3068 * Evaluate the expression for "[var, var] = expr" assignment.
3069 * A line break may follow the assignment operator "=".
3070 */
3071 static char_u *
3072compile_assign_list_expr(cctx_T *cctx, cac_T *cac)
3073{
3074 char_u *whitep;
3075
3076 whitep = cac->cac_op + cac->cac_oplen;
3077
3078 if (may_get_next_line_error(whitep, &cac->cac_nextc, cctx) == FAIL)
3079 return NULL;
3080
3081 // compile RHS expression
3082 if (compile_expr0(&cac->cac_nextc, cctx) == FAIL)
3083 return NULL;
3084
3085 if (cctx->ctx_skip == SKIP_YES)
3086 // no need to parse more when skipping
3087 return cac->cac_nextc;
3088
3089 if (compile_assign_list_check_rhs_type(cctx, cac) == FAIL)
3090 return NULL;
3091
3092 // If a constant list was used we can check the length right here.
3093 if (compile_assign_list_check_length(cctx, cac) == FAIL)
3094 return FAIL;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003095
3096 return cac->cac_nextc;
3097}
3098
3099/*
3100 * Find and return the end of a heredoc or a list of variables assignment
3101 * statement. For a single variable assignment statement, returns the current
3102 * end.
3103 * Returns NULL on failure.
3104 */
3105 static char_u *
3106compile_assign_compute_end(
3107 exarg_T *eap,
3108 cctx_T *cctx,
3109 cac_T *cac,
3110 int heredoc)
3111{
3112 if (heredoc)
3113 {
3114 cac->cac_nextc = parse_heredoc_assignment(eap, cctx, cac);
3115 return cac->cac_nextc;
3116 }
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003117
3118 if (cac->cac_var_count > 0)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003119 {
3120 // for "[var, var] = expr" evaluate the expression. The list of
3121 // variables are processed later.
3122 // A line break may follow the "=".
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003123 cac->cac_nextc = compile_assign_list_expr(cctx, cac);
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003124 return cac->cac_nextc;
3125 }
3126
3127 return cac->cac_var_end;
3128}
3129
3130/*
3131 * For "var = expr" evaluate the expression.
3132 */
3133 static int
3134compile_assign_single_eval_expr(cctx_T *cctx, cac_T *cac)
3135{
3136 int ret = OK;
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003137 char_u *whitep;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003138 lhs_T *lhs = &cac->cac_lhs;
3139
3140 // Compile the expression.
3141 if (cac->cac_incdec)
3142 return generate_PUSHNR(cctx, 1);
3143
3144 // Temporarily hide the new local variable here, it is
3145 // not available to this expression.
3146 if (lhs->lhs_new_local)
3147 --cctx->ctx_locals.ga_len;
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003148 whitep = cac->cac_op + cac->cac_oplen;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003149
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003150 if (may_get_next_line_error(whitep, &cac->cac_nextc, cctx) == FAIL)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003151 {
3152 if (lhs->lhs_new_local)
3153 ++cctx->ctx_locals.ga_len;
3154 return FAIL;
3155 }
3156
3157 ret = compile_expr0_ext(&cac->cac_nextc, cctx, &cac->cac_is_const);
3158 if (lhs->lhs_new_local)
3159 ++cctx->ctx_locals.ga_len;
3160
3161 return ret;
3162}
3163
3164/*
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003165 * When compiling an assignment, set the LHS type to the RHS type.
3166 */
3167 static int
3168compile_assign_set_lhs_type_from_rhs(
3169 cctx_T *cctx,
3170 cac_T *cac,
3171 lhs_T *lhs,
3172 type_T *rhs_type)
3173{
3174 if (rhs_type->tt_type == VAR_VOID)
3175 {
3176 emsg(_(e_cannot_use_void_value));
3177 return FAIL;
3178 }
3179
3180 type_T *type;
3181
3182 // An empty list or dict has a &t_unknown member, for a variable that
3183 // implies &t_any.
3184 if (rhs_type == &t_list_empty)
3185 type = &t_list_any;
3186 else if (rhs_type == &t_dict_empty)
3187 type = &t_dict_any;
3188 else if (rhs_type == &t_unknown)
3189 type = &t_any;
3190 else
3191 {
3192 type = rhs_type;
3193 cac->cac_inferred_type = rhs_type;
3194 }
3195
3196 set_var_type(lhs->lhs_lvar, type, cctx);
3197
3198 return OK;
3199}
3200
3201/*
3202 * Returns TRUE if the "rhs_type" can be assigned to the "lhs" variable.
3203 * Used when compiling an assignment statement.
3204 */
3205 static int
3206compile_assign_valid_rhs_type(
3207 cctx_T *cctx,
3208 cac_T *cac,
3209 lhs_T *lhs,
3210 type_T *rhs_type)
3211{
3212 type_T *use_type = lhs->lhs_lvar->lv_type;
3213 where_T where = WHERE_INIT;
3214
3215 // Without operator check type here, otherwise below.
3216 // Use the line number of the assignment.
3217 SOURCING_LNUM = cac->cac_start_lnum;
3218 if (cac->cac_var_count > 0)
3219 {
3220 where.wt_index = cac->cac_var_idx + 1;
3221 where.wt_kind = WT_VARIABLE;
3222 }
3223
3224 // If assigning to a list or dict member, use the member type.
3225 // Not for "list[:] =".
3226 if (lhs->lhs_has_index &&
3227 !has_list_index(cac->cac_var_start + lhs->lhs_varlen, cctx))
3228 use_type = lhs->lhs_member_type;
3229
3230 if (need_type_where(rhs_type, use_type, FALSE, -1, where, cctx, FALSE,
3231 cac->cac_is_const) == FAIL)
3232 return FALSE;
3233
3234 return TRUE;
3235}
3236
3237/*
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003238 * Compare the LHS type with the RHS type in an assignment.
3239 */
3240 static int
3241compile_assign_check_type(cctx_T *cctx, cac_T *cac)
3242{
3243 lhs_T *lhs = &cac->cac_lhs;
3244 type_T *rhs_type;
3245
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003246 rhs_type = cctx->ctx_type_stack.ga_len == 0
3247 ? &t_void
3248 : get_type_on_stack(cctx, 0);
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003249 cac->cac_rhs_type = rhs_type;
3250
3251 if (check_type_is_value(rhs_type) == FAIL)
3252 return FAIL;
3253
3254 if (lhs->lhs_lvar != NULL && (cac->cac_is_decl || !lhs->lhs_has_type))
3255 {
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003256 if (rhs_type->tt_type == VAR_FUNC
3257 || rhs_type->tt_type == VAR_PARTIAL)
3258 {
3259 // Make sure the variable name can be used as a funcref
3260 if (!lhs->lhs_has_index
3261 && var_wrong_func_name(lhs->lhs_name, TRUE))
3262 return FAIL;
3263 }
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003264
3265 if (lhs->lhs_new_local && !lhs->lhs_has_type)
3266 {
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003267 // The LHS variable doesn't have a type. Set it to the RHS type.
3268 if (compile_assign_set_lhs_type_from_rhs(cctx, cac, lhs, rhs_type)
3269 == FAIL)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003270 return FAIL;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003271 }
3272 else if (*cac->cac_op == '=')
3273 {
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003274 if (!compile_assign_valid_rhs_type(cctx, cac, lhs, rhs_type))
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003275 return FAIL;
3276 }
3277 }
3278 else
3279 {
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003280 // Assigning to a register using @r = "abc"
3281
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003282 type_T *lhs_type = lhs->lhs_member_type;
3283
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003284 // Special case: assigning to @# can use a number or a string.
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003285 // Also: can assign a number to a float.
3286 if ((lhs_type == &t_number_or_string || lhs_type == &t_float)
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003287 && rhs_type->tt_type == VAR_NUMBER)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003288 lhs_type = &t_number;
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003289
3290 if (*cac->cac_nextc != '=')
3291 {
3292 if (need_type(rhs_type, lhs_type, FALSE, -1, 0, cctx, FALSE,
3293 FALSE) == FAIL)
3294 return FAIL;
3295 }
3296 }
3297
3298 return OK;
3299}
3300
3301/*
3302 * Compile the RHS expression in an assignment statement and generate the
3303 * instructions.
3304 */
3305 static int
3306compile_assign_rhs_expr(cctx_T *cctx, cac_T *cac)
3307{
3308 cac->cac_is_const = FALSE;
3309
3310 // for "+=", "*=", "..=" etc. first load the current value
3311 if (*cac->cac_op != '='
3312 && compile_load_lhs_with_index(&cac->cac_lhs, cac->cac_var_start,
3313 cctx) == FAIL)
3314 return FAIL;
3315
3316 // For "var = expr" evaluate the expression.
3317 if (cac->cac_var_count == 0)
3318 {
3319 int ret;
3320
3321 // Compile the expression.
3322 cac->cac_instr_count = cac->cac_instr->ga_len;
3323 ret = compile_assign_single_eval_expr(cctx, cac);
3324 if (ret == FAIL)
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003325 return FAIL;
3326 }
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003327 else if (cac->cac_semicolon && cac->cac_var_idx == cac->cac_var_count - 1)
3328 {
3329 // For "[var; var] = expr" get the rest of the list
3330 cac->cac_did_generate_slice = TRUE;
3331 if (generate_SLICE(cctx, cac->cac_var_count - 1) == FAIL)
3332 return FAIL;
3333 }
3334 else
3335 {
3336 // For "[var, var] = expr" get the "var_idx" item from the
3337 // list.
3338 int with_op = *cac->cac_op != '=';
3339 if (generate_GETITEM(cctx, cac->cac_var_idx, with_op) == FAIL)
3340 return FAIL;
3341 }
3342
3343 if (compile_assign_check_type(cctx, cac) == FAIL)
3344 return FAIL;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003345
3346 return OK;
3347}
3348
3349/*
3350 * Compile the RHS expression in an assignment
3351 */
3352 static int
3353compile_assign_rhs(cctx_T *cctx, cac_T *cac)
3354{
3355 lhs_T *lhs = &cac->cac_lhs;
3356
3357 if (cctx->ctx_skip == SKIP_YES)
3358 {
3359 if (cac->cac_oplen > 0 && cac->cac_var_count == 0)
3360 {
3361 // skip over the "=" and the expression
3362 cac->cac_nextc = skipwhite(cac->cac_op + cac->cac_oplen);
3363 (void)compile_expr0(&cac->cac_nextc, cctx);
3364 }
3365 return OK;
3366 }
3367
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003368 // If RHS is specified, then generate instructions for RHS expression
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003369 if (cac->cac_oplen > 0)
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003370 return compile_assign_rhs_expr(cctx, cac);
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003371
3372 if (cac->cac_cmdidx == CMD_final)
3373 {
3374 emsg(_(e_final_requires_a_value));
3375 return FAIL;
3376 }
3377
3378 if (cac->cac_cmdidx == CMD_const)
3379 {
3380 emsg(_(e_const_requires_a_value));
3381 return FAIL;
3382 }
3383
3384 if (!lhs->lhs_has_type || lhs->lhs_dest == dest_option
3385 || lhs->lhs_dest == dest_func_option)
3386 {
3387 emsg(_(e_type_or_initialization_required));
3388 return FAIL;
3389 }
3390
3391 // variables are always initialized
3392 if (GA_GROW_FAILS(cac->cac_instr, 1))
3393 return FAIL;
3394
3395 cac->cac_instr_count = cac->cac_instr->ga_len;
3396
3397 return push_default_value(cctx, lhs->lhs_member_type->tt_type,
3398 lhs->lhs_dest == dest_local,
3399 &cac->cac_skip_store);
3400}
3401
3402/*
3403 * Compile a compound op assignment statement (+=, -=, *=, %=, etc.)
3404 */
3405 static int
3406compile_assign_compound_op(cctx_T *cctx, cac_T *cac)
3407{
3408 lhs_T *lhs = &cac->cac_lhs;
3409 type_T *expected;
3410 type_T *stacktype = NULL;
3411
3412 if (*cac->cac_op == '.')
3413 {
3414 if (may_generate_2STRING(-1, TOSTRING_NONE, cctx) == FAIL)
3415 return FAIL;
3416 }
3417 else
3418 {
3419 expected = lhs->lhs_member_type;
3420 stacktype = get_type_on_stack(cctx, 0);
3421 if (
3422 // If variable is float operation with number is OK.
3423 !(expected == &t_float && (stacktype == &t_number
3424 || stacktype == &t_number_bool))
3425 && need_type(stacktype, expected, TRUE, -1, 0, cctx,
3426 FALSE, FALSE) == FAIL)
3427 return FAIL;
3428 }
3429
3430 if (*cac->cac_op == '.')
3431 {
3432 if (generate_CONCAT(cctx, 2) == FAIL)
3433 return FAIL;
3434 }
3435 else if (*cac->cac_op == '+')
3436 {
3437 if (generate_add_instr(cctx,
3438 operator_type(lhs->lhs_member_type, stacktype),
3439 lhs->lhs_member_type, stacktype,
3440 EXPR_APPEND) == FAIL)
3441 return FAIL;
3442 }
3443 else if (generate_two_op(cctx, cac->cac_op) == FAIL)
3444 return FAIL;
3445
3446 return OK;
3447}
3448
3449/*
3450 * Generate the STORE and SETTYPE instructions for an assignment statement.
3451 */
3452 static int
3453compile_assign_generate_store(cctx_T *cctx, cac_T *cac)
3454{
3455 lhs_T *lhs = &cac->cac_lhs;
3456 int save_lnum;
3457
3458 // Use the line number of the assignment for store instruction.
3459 save_lnum = cctx->ctx_lnum;
3460 cctx->ctx_lnum = cac->cac_start_lnum - 1;
3461
3462 if (lhs->lhs_has_index)
3463 {
3464 // Use the info in "lhs" to store the value at the index in the
3465 // list, dict or object.
3466 if (compile_assign_unlet(cac->cac_var_start, &cac->cac_lhs,
3467 TRUE, cac->cac_rhs_type, cctx) == FAIL)
3468 {
3469 cctx->ctx_lnum = save_lnum;
3470 return FAIL;
3471 }
3472 }
3473 else
3474 {
3475 if (cac->cac_is_decl && cac->cac_cmdidx == CMD_const &&
3476 (lhs->lhs_dest == dest_script
3477 || lhs->lhs_dest == dest_script_v9
3478 || lhs->lhs_dest == dest_global
3479 || lhs->lhs_dest == dest_local))
3480 // ":const var": lock the value, but not referenced variables
3481 generate_LOCKCONST(cctx);
3482
3483 type_T *inferred_type = cac->cac_inferred_type;
3484
3485 if ((lhs->lhs_type->tt_type == VAR_DICT
3486 || lhs->lhs_type->tt_type == VAR_LIST)
3487 && lhs->lhs_type->tt_member != NULL
3488 && lhs->lhs_type->tt_member != &t_any
3489 && lhs->lhs_type->tt_member != &t_unknown)
3490 // Set the type in the list or dict, so that it can be checked,
3491 // also in legacy script.
3492 generate_SETTYPE(cctx, lhs->lhs_type);
3493 else if (inferred_type != NULL
3494 && (inferred_type->tt_type == VAR_DICT
3495 || inferred_type->tt_type == VAR_LIST)
3496 && inferred_type->tt_member != NULL
3497 && inferred_type->tt_member != &t_unknown
3498 && inferred_type->tt_member != &t_any)
3499 // Set the type in the list or dict, so that it can be checked,
3500 // also in legacy script.
3501 generate_SETTYPE(cctx, inferred_type);
3502
3503 if (!cac->cac_skip_store &&
3504 generate_store_lhs(cctx, &cac->cac_lhs,
3505 cac->cac_instr_count,
3506 cac->cac_is_decl) == FAIL)
3507 {
3508 cctx->ctx_lnum = save_lnum;
3509 return FAIL;
3510 }
3511 }
3512
3513 cctx->ctx_lnum = save_lnum;
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003514
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003515 return OK;
3516}
3517
3518/*
3519 * Process the variable(s) in an assignment statement
3520 */
3521 static int
3522compile_assign_process_variables(
3523 cctx_T *cctx,
3524 cac_T *cac,
3525 int cmdidx,
3526 int heredoc,
3527 int has_cmd,
3528 int has_argisset_prefix,
3529 int jump_instr_idx)
3530{
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003531 /*
3532 * Loop over variables in "[var, var] = expr".
3533 * For "name = expr" and "var name: type" this is done only once.
3534 */
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003535 for (cac->cac_var_idx = 0; cac->cac_var_idx == 0 ||
3536 cac->cac_var_idx < cac->cac_var_count; cac->cac_var_idx++)
3537 {
3538 if (cac->cac_var_start[0] == '_'
3539 && !eval_isnamec(cac->cac_var_start[1]))
3540 {
3541 // Ignore underscore in "[a, _, b] = list".
3542 if (cac->cac_var_count > 0)
3543 {
3544 cac->cac_var_start = skipwhite(cac->cac_var_start + 2);
3545 continue;
3546 }
3547 emsg(_(e_cannot_use_underscore_here));
3548 return FAIL;
3549 }
3550 vim_free(cac->cac_lhs.lhs_name);
3551
3552 /*
3553 * Figure out the LHS type and other properties.
3554 */
3555 if (compile_assign_lhs(cac->cac_var_start, &cac->cac_lhs, cmdidx,
3556 cac->cac_is_decl, heredoc, has_cmd,
3557 cac->cac_oplen, cctx) == FAIL)
3558 return FAIL;
3559
3560 // Compile the RHS expression
3561 if (heredoc)
3562 {
3563 SOURCING_LNUM = cac->cac_start_lnum;
3564 if (cac->cac_lhs.lhs_has_type
3565 && need_type(&t_list_string, cac->cac_lhs.lhs_type,
3566 FALSE, -1, 0, cctx, FALSE, FALSE) == FAIL)
3567 return FAIL;
3568 }
3569 else
3570 {
3571 if (compile_assign_rhs(cctx, cac) == FAIL)
3572 return FAIL;
3573 if (cac->cac_var_count == 0)
3574 cac->cac_var_end = cac->cac_nextc;
3575 }
3576
3577 // no need to parse more when skipping
3578 if (cctx->ctx_skip == SKIP_YES)
3579 break;
3580
3581 if (cac->cac_oplen > 0 && *cac->cac_op != '=')
3582 {
3583 if (compile_assign_compound_op(cctx, cac) == FAIL)
3584 return FAIL;
3585 }
3586
3587 // generate the store instructions
3588 if (compile_assign_generate_store(cctx, cac) == FAIL)
3589 return FAIL;
3590
3591 if (cac->cac_var_idx + 1 < cac->cac_var_count)
3592 cac->cac_var_start = skipwhite(cac->cac_lhs.lhs_end + 1);
3593
3594 if (has_argisset_prefix)
3595 {
3596 // set instruction index in JUMP_IF_ARG_SET to here
3597 isn_T *isn = ((isn_T *)cac->cac_instr->ga_data) + jump_instr_idx;
3598 isn->isn_arg.jumparg.jump_where = cac->cac_instr->ga_len;
3599 }
3600 }
3601
3602 return OK;
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003603}
3604
3605/*
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003606 * Compile declaration and assignment:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003607 * "let name"
3608 * "var name = expr"
3609 * "final name = expr"
3610 * "const name = expr"
3611 * "name = expr"
3612 * "arg" points to "name".
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003613 * "++arg" and "--arg"
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003614 * Return NULL for an error.
3615 * Return "arg" if it does not look like a variable list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003616 */
3617 static char_u *
Bram Moolenaar65b0d162022-12-13 18:43:22 +00003618compile_assignment(
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003619 char_u *arg_start,
3620 exarg_T *eap,
3621 cmdidx_T cmdidx,
3622 cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003623{
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003624 cac_T cac;
Bram Moolenaar65b0d162022-12-13 18:43:22 +00003625 char_u *arg = arg_start;
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003626 char_u *retstr = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003627 int heredoc = FALSE;
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003628 int jump_instr_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003630 compile_assign_context_init(&cac, cctx, cmdidx, arg);
3631
3632 jump_instr_idx = cac.cac_instr->ga_len;
3633
3634 // process object variable initialization in a new() constructor method
3635 int has_argisset_prefix = STRNCMP(arg, "ifargisset ", 11) == 0;
3636 if (has_argisset_prefix &&
3637 compile_assign_obj_new_arg(&arg, cctx) == FAIL)
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003638 goto theend;
Bram Moolenaar65b0d162022-12-13 18:43:22 +00003639
Bram Moolenaare1d12112022-03-05 11:37:48 +00003640 // Skip over the "varname" or "[varname, varname]" to get to any "=".
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003641 cac.cac_nextc = skip_var_list(arg, TRUE, &cac.cac_var_count,
3642 &cac.cac_semicolon, TRUE);
3643 if (cac.cac_nextc == NULL)
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003644 return *arg == '[' ? arg : NULL;
3645
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003646 if (compile_assign_process_operator(eap, arg, &cac, &heredoc,
3647 &retstr) == FAIL)
3648 return retstr;
Yegappan Lakshmanan85ee7422024-12-08 10:15:35 +01003649
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003650 // Compute the start of the assignment
3651 cac.cac_var_start = compile_assign_compute_start(arg, cac.cac_var_count);
3652
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003653 // Compute the end of the assignment
3654 cac.cac_var_end = compile_assign_compute_end(eap, cctx, &cac, heredoc);
3655 if (cac.cac_var_end == NULL)
3656 return NULL;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003657
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003658 int has_cmd = cac.cac_var_start > eap->cmd;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003659
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003660 /* process the variable(s) */
3661 if (compile_assign_process_variables(cctx, &cac, cmdidx, heredoc,
3662 has_cmd, has_argisset_prefix,
3663 jump_instr_idx) == FAIL)
3664 goto theend;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003665
Bram Moolenaar4d5dfe22021-06-26 13:59:29 +02003666 // For "[var, var] = expr" drop the "expr" value.
3667 // Also for "[var, var; _] = expr".
Yegappan Lakshmanane2038412024-12-14 19:59:24 +01003668 if (cctx->ctx_skip != SKIP_YES && cac.cac_var_count > 0 &&
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003669 (!cac.cac_semicolon || !cac.cac_did_generate_slice))
Bram Moolenaar9af78762020-06-16 11:34:42 +02003670 {
Bram Moolenaarec792292020-12-13 21:26:56 +01003671 if (generate_instr_drop(cctx, ISN_DROP, 1) == NULL)
Bram Moolenaar9af78762020-06-16 11:34:42 +02003672 goto theend;
3673 }
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003674
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003675 retstr = skipwhite(cac.cac_var_end);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676
3677theend:
Yegappan Lakshmanan95a03fc2024-12-13 11:54:54 +01003678 vim_free(cac.cac_lhs.lhs_name);
Yegappan Lakshmanan468db1f2024-12-16 20:56:56 +01003679 return retstr;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003680}
3681
3682/*
Bram Moolenaar17126b12021-01-07 22:03:02 +01003683 * Check for an assignment at "eap->cmd", compile it if found.
3684 * Return NOTDONE if there is none, FAIL for failure, OK if done.
3685 */
3686 static int
3687may_compile_assignment(exarg_T *eap, char_u **line, cctx_T *cctx)
3688{
3689 char_u *pskip;
3690 char_u *p;
3691
3692 // Assuming the command starts with a variable or function name,
3693 // find what follows.
3694 // Skip over "var.member", "var[idx]" and the like.
3695 // Also "&opt = val", "$ENV = val" and "@r = val".
3696 pskip = (*eap->cmd == '&' || *eap->cmd == '$' || *eap->cmd == '@')
3697 ? eap->cmd + 1 : eap->cmd;
3698 p = to_name_end(pskip, TRUE);
3699 if (p > eap->cmd && *p != NUL)
3700 {
3701 char_u *var_end;
3702 int oplen;
3703 int heredoc;
3704
3705 if (eap->cmd[0] == '@')
3706 var_end = eap->cmd + 2;
3707 else
3708 var_end = find_name_end(pskip, NULL, NULL,
3709 FNE_CHECK_START | FNE_INCL_BR);
3710 oplen = assignment_len(skipwhite(var_end), &heredoc);
3711 if (oplen > 0)
3712 {
3713 size_t len = p - eap->cmd;
3714
3715 // Recognize an assignment if we recognize the variable
3716 // name:
Bram Moolenaar17126b12021-01-07 22:03:02 +01003717 // "&opt = expr"
3718 // "$ENV = expr"
3719 // "@r = expr"
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003720 // "g:var = expr"
3721 // "g:[key] = expr"
3722 // "local = expr" where "local" is a local var.
3723 // "script = expr" where "script" is a script-local var.
3724 // "import = expr" where "import" is an imported var
Bram Moolenaar17126b12021-01-07 22:03:02 +01003725 if (*eap->cmd == '&'
3726 || *eap->cmd == '$'
3727 || *eap->cmd == '@'
3728 || ((len) > 2 && eap->cmd[1] == ':')
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00003729 || STRNCMP(eap->cmd, "g:[", 3) == 0
Bram Moolenaare0890d62021-02-17 14:52:14 +01003730 || variable_exists(eap->cmd, len, cctx))
Bram Moolenaar17126b12021-01-07 22:03:02 +01003731 {
3732 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
3733 if (*line == NULL || *line == eap->cmd)
3734 return FAIL;
3735 return OK;
3736 }
3737 }
3738 }
3739
Bram Moolenaar65b0d162022-12-13 18:43:22 +00003740 // might be "[var, var] = expr" or "ifargisset this.member = expr"
3741 if (*eap->cmd == '[' || STRNCMP(eap->cmd, "ifargisset ", 11) == 0)
Bram Moolenaar17126b12021-01-07 22:03:02 +01003742 {
Bram Moolenaar17126b12021-01-07 22:03:02 +01003743 *line = compile_assignment(eap->cmd, eap, CMD_SIZE, cctx);
3744 if (*line == NULL)
3745 return FAIL;
3746 if (*line != eap->cmd)
3747 return OK;
3748 }
3749 return NOTDONE;
3750}
3751
Bram Moolenaar9a015112021-12-31 14:06:45 +00003752/*
3753 * Check if arguments of "ufunc" shadow variables in "cctx".
3754 * Return OK or FAIL.
3755 */
3756 static int
3757check_args_shadowing(ufunc_T *ufunc, cctx_T *cctx)
3758{
3759 int i;
3760 char_u *arg;
3761 int r = OK;
3762
3763 // Make sure arguments are not found when compiling a second time.
3764 ufunc->uf_args_visible = 0;
3765
3766 // Check for arguments shadowing variables from the context.
3767 for (i = 0; i < ufunc->uf_args.ga_len; ++i)
3768 {
3769 arg = ((char_u **)(ufunc->uf_args.ga_data))[i];
Bram Moolenaardce24412022-02-08 20:35:30 +00003770 if (check_defined(arg, STRLEN(arg), cctx, NULL, TRUE) == FAIL)
Bram Moolenaar9a015112021-12-31 14:06:45 +00003771 {
3772 r = FAIL;
3773 break;
3774 }
3775 }
3776 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
3777 return r;
3778}
3779
Bram Moolenaarfcb86b02022-10-07 22:46:24 +01003780#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaar139575d2022-03-15 19:29:30 +00003781/*
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003782 * Get a count before a command. Can only be a number.
3783 * Returns zero if there is no count.
3784 * Returns -1 if there is something wrong.
3785 */
3786 static long
3787get_cmd_count(char_u *line, exarg_T *eap)
3788{
3789 char_u *p;
3790
3791 // skip over colons and white space
3792 for (p = line; *p == ':' || VIM_ISWHITE(*p); ++p)
3793 ;
Keith Thompson184f71c2024-01-04 21:19:04 +01003794 if (!SAFE_isdigit(*p))
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003795 {
Bram Moolenaar2435adf2022-10-21 12:05:46 +01003796 // The command or modifiers must be following. Assume a lower case
3797 // character means there is a modifier.
3798 if (p < eap->cmd && !vim_islower(*p))
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003799 {
3800 emsg(_(e_invalid_range));
3801 return -1;
3802 }
3803 return 0;
3804 }
3805 return atol((char *)p);
3806}
Bram Moolenaarfcb86b02022-10-07 22:46:24 +01003807#endif
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003808
3809/*
Bram Moolenaar139575d2022-03-15 19:29:30 +00003810 * Get the compilation type that should be used for "ufunc".
3811 * Keep in sync with INSTRUCTIONS().
3812 */
3813 compiletype_T
3814get_compile_type(ufunc_T *ufunc)
3815{
3816 // Update uf_has_breakpoint if needed.
3817 update_has_breakpoint(ufunc);
3818
3819 if (debug_break_level > 0 || may_break_in_function(ufunc))
3820 return CT_DEBUG;
3821#ifdef FEAT_PROFILE
3822 if (do_profiling == PROF_YES)
3823 {
Ernie Rael21d32122023-09-02 15:09:18 +02003824 if (!ufunc->uf_profiling && has_profiling(FALSE, ufunc->uf_name, NULL,
3825 &ufunc->uf_hash))
Bram Moolenaar139575d2022-03-15 19:29:30 +00003826 func_do_profile(ufunc);
3827 if (ufunc->uf_profiling)
3828 return CT_PROFILE;
3829 }
3830#endif
3831 return CT_NONE;
3832}
3833
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01003834/*
3835 * Free the compiled instructions saved for a def function. This is used when
3836 * compiling a def function and the function was compiled before.
3837 * The index is reused.
3838 */
3839 static void
3840clear_def_function(ufunc_T *ufunc, compiletype_T compile_type)
3841{
3842 isn_T *instr_dest = NULL;
3843 dfunc_T *dfunc;
3844
3845 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
3846
3847 switch (compile_type)
3848 {
3849 case CT_PROFILE:
3850#ifdef FEAT_PROFILE
3851 instr_dest = dfunc->df_instr_prof; break;
3852#endif
3853 case CT_NONE: instr_dest = dfunc->df_instr; break;
3854 case CT_DEBUG: instr_dest = dfunc->df_instr_debug; break;
3855 }
3856
3857 if (instr_dest != NULL)
3858 // Was compiled in this mode before: Free old instructions.
3859 delete_def_function_contents(dfunc, FALSE);
3860
3861 ga_clear_strings(&dfunc->df_var_names);
3862 dfunc->df_defer_var_idx = 0;
3863}
Bram Moolenaar7b829262021-10-13 15:04:34 +01003864
3865/*
Bram Moolenaar09689a02020-05-09 22:50:08 +02003866 * Add a function to the list of :def functions.
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003867 * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
Bram Moolenaar09689a02020-05-09 22:50:08 +02003868 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02003869 static int
Bram Moolenaar09689a02020-05-09 22:50:08 +02003870add_def_function(ufunc_T *ufunc)
3871{
3872 dfunc_T *dfunc;
3873
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003874 if (def_functions.ga_len == 0)
3875 {
3876 // The first position is not used, so that a zero uf_dfunc_idx means it
3877 // wasn't set.
Bram Moolenaar35578162021-08-02 19:10:38 +02003878 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003879 return FAIL;
3880 ++def_functions.ga_len;
3881 }
3882
Bram Moolenaar09689a02020-05-09 22:50:08 +02003883 // Add the function to "def_functions".
Bram Moolenaar35578162021-08-02 19:10:38 +02003884 if (GA_GROW_FAILS(&def_functions, 1))
Bram Moolenaar09689a02020-05-09 22:50:08 +02003885 return FAIL;
3886 dfunc = ((dfunc_T *)def_functions.ga_data) + def_functions.ga_len;
3887 CLEAR_POINTER(dfunc);
3888 dfunc->df_idx = def_functions.ga_len;
3889 ufunc->uf_dfunc_idx = dfunc->df_idx;
3890 dfunc->df_ufunc = ufunc;
John Marriottb32800f2025-02-01 15:25:34 +01003891 dfunc->df_name = vim_strnsave(ufunc->uf_name, ufunc->uf_namelen);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02003892 ga_init2(&dfunc->df_var_names, sizeof(char_u *), 10);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003893 ++dfunc->df_refcount;
Bram Moolenaar09689a02020-05-09 22:50:08 +02003894 ++def_functions.ga_len;
3895 return OK;
3896}
3897
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01003898 static int
3899compile_dfunc_ufunc_init(
3900 ufunc_T *ufunc,
3901 cctx_T *outer_cctx,
3902 compiletype_T compile_type,
3903 int *new_def_function)
3904{
3905 // When using a function that was compiled before: Free old instructions.
3906 // The index is reused. Otherwise add a new entry in "def_functions".
3907 if (ufunc->uf_dfunc_idx > 0)
3908 clear_def_function(ufunc, compile_type);
3909 else
3910 {
3911 if (add_def_function(ufunc) == FAIL)
3912 return FAIL;
3913
3914 *new_def_function = TRUE;
3915 }
3916
3917 if ((ufunc->uf_flags & FC_CLOSURE) && outer_cctx == NULL)
3918 {
3919 semsg(_(e_compiling_closure_without_context_str),
3920 printable_func_name(ufunc));
3921 return FAIL;
3922 }
3923
3924 ufunc->uf_def_status = UF_COMPILING;
3925
3926 return OK;
3927}
3928
3929/*
3930 * Initialize the compilation context for compiling a def function.
3931 */
3932 static void
3933compile_dfunc_cctx_init(
3934 cctx_T *cctx,
3935 cctx_T *outer_cctx,
3936 ufunc_T *ufunc,
3937 compiletype_T compile_type)
3938{
3939 CLEAR_FIELD(*cctx);
3940
3941 cctx->ctx_compile_type = compile_type;
3942 cctx->ctx_ufunc = ufunc;
3943 cctx->ctx_lnum = -1;
3944 cctx->ctx_outer = outer_cctx;
3945 ga_init2(&cctx->ctx_locals, sizeof(lvar_T), 10);
3946 // Each entry on the type stack consists of two type pointers.
3947 ga_init2(&cctx->ctx_type_stack, sizeof(type2_T), 50);
3948 cctx->ctx_type_list = &ufunc->uf_type_list;
3949 ga_init2(&cctx->ctx_instr, sizeof(isn_T), 50);
3950}
3951
Bram Moolenaar09689a02020-05-09 22:50:08 +02003952/*
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02003953 * For an object constructor, generate instruction to setup "this" (the first
3954 * local variable) and to initialize the object variables.
3955 */
3956 static int
3957obj_constructor_prologue(ufunc_T *ufunc, cctx_T *cctx)
3958{
3959 generate_CONSTRUCT(cctx, ufunc->uf_class);
3960
3961 for (int i = 0; i < ufunc->uf_class->class_obj_member_count; ++i)
3962 {
3963 ocmember_T *m = &ufunc->uf_class->class_obj_members[i];
3964
3965 if (i < 2 && IS_ENUM(ufunc->uf_class))
3966 // The first two object variables in an enum are the name
3967 // and the ordinal. These are set by the ISN_CONSTRUCT
3968 // instruction. So don't generate instructions to set
3969 // these variables.
3970 continue;
3971
3972 if (m->ocm_init != NULL)
3973 {
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +01003974 char_u *expr = m->ocm_init;
3975 sctx_T save_current_sctx;
3976 int change_sctx = FALSE;
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02003977
Yegappan Lakshmanan16f2d3a2025-02-24 19:23:43 +01003978 // If the member variable initialization script context is
3979 // different from the current script context, then change it.
3980 if (current_sctx.sc_sid != m->ocm_init_sctx.sc_sid)
3981 change_sctx = TRUE;
3982
3983 if (change_sctx)
3984 {
3985 // generate an instruction to change the script context to the
3986 // member variable initialization script context.
3987 save_current_sctx = current_sctx;
3988 current_sctx = m->ocm_init_sctx;
3989 generate_SCRIPTCTX_SET(cctx, current_sctx);
3990 }
3991
3992 int r = compile_expr0(&expr, cctx);
3993
3994 if (change_sctx)
3995 {
3996 // restore the previous script context
3997 current_sctx = save_current_sctx;
3998 generate_SCRIPTCTX_SET(cctx, current_sctx);
3999 }
4000
4001 if (r == FAIL)
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004002 return FAIL;
4003
4004 if (!ends_excmd2(m->ocm_init, expr))
4005 {
4006 semsg(_(e_trailing_characters_str), expr);
4007 return FAIL;
4008 }
4009
4010 type_T *type = get_type_on_stack(cctx, 0);
4011 if (m->ocm_type->tt_type == VAR_ANY
4012 && !(m->ocm_flags & OCMFLAG_HAS_TYPE)
4013 && type->tt_type != VAR_SPECIAL)
4014 {
4015 // If the member variable type is not yet set, then use
4016 // the initialization expression type.
4017 m->ocm_type = type;
4018 }
LemonBoyf4af3312024-07-04 13:43:12 +02004019 else
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004020 {
4021 // The type of the member initialization expression is
4022 // determined at run time. Add a runtime type check.
4023 where_T where = WHERE_INIT;
4024 where.wt_kind = WT_MEMBER;
4025 where.wt_func_name = (char *)m->ocm_name;
4026 if (need_type_where(type, m->ocm_type, FALSE, -1,
4027 where, cctx, FALSE, FALSE) == FAIL)
4028 return FAIL;
4029 }
4030 }
4031 else
4032 push_default_value(cctx, m->ocm_type->tt_type, FALSE, NULL);
4033
LemonBoyf4af3312024-07-04 13:43:12 +02004034 if ((m->ocm_type->tt_type == VAR_DICT
4035 || m->ocm_type->tt_type == VAR_LIST)
4036 && m->ocm_type->tt_member != NULL
4037 && m->ocm_type->tt_member != &t_any
4038 && m->ocm_type->tt_member != &t_unknown)
4039 // Set the type in the list or dict, so that it can be checked,
4040 // also in legacy script.
4041 generate_SETTYPE(cctx, m->ocm_type);
4042
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004043 generate_STORE_THIS(cctx, i);
4044 }
4045
4046 return OK;
4047}
4048
4049/*
4050 * For an object method and an constructor, generate instruction to setup
4051 * "this" (the first local variable). For a constructor, generate instructions
4052 * to initialize the object variables.
4053 */
4054 static int
4055obj_method_prologue(ufunc_T *ufunc, cctx_T *cctx)
4056{
4057 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
4058
4059 if (GA_GROW_FAILS(&dfunc->df_var_names, 1))
4060 return FAIL;
4061
4062 ((char_u **)dfunc->df_var_names.ga_data)[0] =
4063 vim_strsave((char_u *)"this");
4064 ++dfunc->df_var_names.ga_len;
4065
4066 // In the constructor allocate memory for the object and initialize the
4067 // object members.
4068 if (IS_CONSTRUCTOR_METHOD(ufunc))
4069 return obj_constructor_prologue(ufunc, cctx);
4070
4071 return OK;
4072}
4073
4074/*
4075 * Produce instructions for the default values of optional arguments.
4076 */
4077 static int
4078compile_def_function_default_args(
4079 ufunc_T *ufunc,
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004080 garray_T *instr,
4081 cctx_T *cctx)
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004082{
4083 int count = ufunc->uf_def_args.ga_len;
4084 int first_def_arg = ufunc->uf_args.ga_len - count;
4085 int i;
4086 int off = STACK_FRAME_SIZE + (ufunc->uf_va_name != NULL ? 1 : 0);
4087 int did_set_arg_type = FALSE;
4088
4089 // Produce instructions for the default values of optional arguments.
4090 SOURCING_LNUM = 0; // line number unknown
4091 for (i = 0; i < count; ++i)
4092 {
4093 char_u *arg = ((char_u **)(ufunc->uf_def_args.ga_data))[i];
4094 if (STRCMP(arg, "v:none") == 0)
4095 // "arg = v:none" means the argument is optional without
4096 // setting a value when the argument is missing.
4097 continue;
4098
4099 type_T *val_type;
4100 int arg_idx = first_def_arg + i;
4101 where_T where = WHERE_INIT;
4102 int jump_instr_idx = instr->ga_len;
4103 isn_T *isn;
4104
4105 // Use a JUMP_IF_ARG_SET instruction to skip if the value was given.
4106 if (generate_JUMP_IF_ARG(cctx, ISN_JUMP_IF_ARG_SET,
4107 i - count - off) == FAIL)
4108 return FAIL;
4109
4110 // Make sure later arguments are not found.
4111 ufunc->uf_args_visible = arg_idx;
4112
4113 int r = compile_expr0(&arg, cctx);
4114 if (r == FAIL)
4115 return FAIL;
4116
4117 // If no type specified use the type of the default value.
4118 // Otherwise check that the default value type matches the
4119 // specified type.
4120 val_type = get_type_on_stack(cctx, 0);
4121 where.wt_index = arg_idx + 1;
4122 where.wt_kind = WT_ARGUMENT;
4123 if (ufunc->uf_arg_types[arg_idx] == &t_unknown)
4124 {
4125 did_set_arg_type = TRUE;
4126 ufunc->uf_arg_types[arg_idx] = val_type;
4127 }
4128 else if (need_type_where(val_type, ufunc->uf_arg_types[arg_idx],
4129 FALSE, -1, where, cctx, FALSE, FALSE) == FAIL)
4130 return FAIL;
4131
4132 if (generate_STORE(cctx, ISN_STORE, i - count - off, NULL) == FAIL)
4133 return FAIL;
4134
4135 // set instruction index in JUMP_IF_ARG_SET to here
4136 isn = ((isn_T *)instr->ga_data) + jump_instr_idx;
4137 isn->isn_arg.jumparg.jump_where = instr->ga_len;
4138 }
4139
4140 if (did_set_arg_type)
4141 set_function_type(ufunc);
4142
4143 return OK;
4144}
4145
4146/*
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004147 * Compile def function body. Loop over all the lines in the function and
4148 * generate instructions.
4149 */
4150 static int
4151compile_def_function_body(
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004152 int last_func_lnum,
4153 int check_return_type,
4154 garray_T *lines_to_free,
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004155 char **errormsg,
4156 cctx_T *cctx)
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004157{
4158 char_u *line = NULL;
4159 char_u *p;
4160 int did_emsg_before = did_emsg;
4161#ifdef FEAT_PROFILE
4162 int prof_lnum = -1;
4163#endif
4164 int debug_lnum = -1;
4165
4166 for (;;)
4167 {
4168 exarg_T ea;
4169 int starts_with_colon = FALSE;
4170 char_u *cmd;
4171 cmdmod_T local_cmdmod;
4172
4173 // Bail out on the first error to avoid a flood of errors and report
4174 // the right line number when inside try/catch.
4175 if (did_emsg_before != did_emsg)
4176 return FAIL;
4177
4178 if (line != NULL && *line == '|')
4179 // the line continues after a '|'
4180 ++line;
4181 else if (line != NULL && *skipwhite(line) != NUL
4182 && !(*line == '#' && (line == cctx->ctx_line_start
4183 || VIM_ISWHITE(line[-1]))))
4184 {
4185 semsg(_(e_trailing_characters_str), line);
4186 return FAIL;
4187 }
4188 else if (line != NULL && vim9_bad_comment(skipwhite(line)))
4189 return FAIL;
4190 else
4191 {
4192 line = next_line_from_context(cctx, FALSE);
4193 if (cctx->ctx_lnum >= last_func_lnum)
4194 {
4195 // beyond the last line
4196#ifdef FEAT_PROFILE
4197 if (cctx->ctx_skip != SKIP_YES)
4198 may_generate_prof_end(cctx, prof_lnum);
4199#endif
4200 break;
4201 }
4202 // Make a copy, splitting off nextcmd and removing trailing spaces
4203 // may change it.
4204 if (line != NULL)
4205 {
4206 line = vim_strsave(line);
4207 if (ga_add_string(lines_to_free, line) == FAIL)
4208 return FAIL;
4209 }
4210 }
4211
4212 CLEAR_FIELD(ea);
4213 ea.cmdlinep = &line;
4214 ea.cmd = skipwhite(line);
4215 ea.skip = cctx->ctx_skip == SKIP_YES;
4216
4217 if (*ea.cmd == '#')
4218 {
4219 // "#" starts a comment, but "#{" is an error
4220 if (vim9_bad_comment(ea.cmd))
4221 return FAIL;
4222 line = (char_u *)"";
4223 continue;
4224 }
4225
4226#ifdef FEAT_PROFILE
4227 if (cctx->ctx_compile_type == CT_PROFILE && cctx->ctx_lnum != prof_lnum
4228 && cctx->ctx_skip != SKIP_YES)
4229 {
4230 may_generate_prof_end(cctx, prof_lnum);
4231
4232 prof_lnum = cctx->ctx_lnum;
4233 generate_instr(cctx, ISN_PROF_START);
4234 }
4235#endif
4236 if (cctx->ctx_compile_type == CT_DEBUG && cctx->ctx_lnum != debug_lnum
4237 && cctx->ctx_skip != SKIP_YES)
4238 {
4239 debug_lnum = cctx->ctx_lnum;
4240 generate_instr_debug(cctx);
4241 }
4242 cctx->ctx_prev_lnum = cctx->ctx_lnum + 1;
4243
4244 // Some things can be recognized by the first character.
4245 switch (*ea.cmd)
4246 {
4247 case '}':
4248 {
4249 // "}" ends a block scope
4250 scopetype_T stype = cctx->ctx_scope == NULL
4251 ? NO_SCOPE : cctx->ctx_scope->se_type;
4252
4253 if (stype == BLOCK_SCOPE)
4254 {
4255 compile_endblock(cctx);
4256 line = ea.cmd;
4257 }
4258 else
4259 {
4260 emsg(_(e_using_rcurly_outside_if_block_scope));
4261 return FAIL;
4262 }
4263 if (line != NULL)
4264 line = skipwhite(ea.cmd + 1);
4265 continue;
4266 }
4267
4268 case '{':
4269 // "{" starts a block scope
4270 // "{'a': 1}->func() is something else
4271 if (ends_excmd(*skipwhite(ea.cmd + 1)))
4272 {
4273 line = compile_block(ea.cmd, cctx);
4274 continue;
4275 }
4276 break;
4277 }
4278
4279 /*
4280 * COMMAND MODIFIERS
4281 */
4282 cctx->ctx_has_cmdmod = FALSE;
4283 if (parse_command_modifiers(&ea, errormsg, &local_cmdmod, FALSE)
4284 == FAIL)
4285 return FAIL;
4286 generate_cmdmods(cctx, &local_cmdmod);
4287 undo_cmdmod(&local_cmdmod);
4288
4289 // Check if there was a colon after the last command modifier or before
4290 // the current position.
4291 for (p = ea.cmd; p >= line; --p)
4292 {
4293 if (*p == ':')
4294 starts_with_colon = TRUE;
4295 if (p < ea.cmd && !VIM_ISWHITE(*p))
4296 break;
4297 }
4298
4299 // Skip ":call" to get to the function name, unless using :legacy
4300 p = ea.cmd;
4301 if (!(local_cmdmod.cmod_flags & CMOD_LEGACY))
4302 {
4303 if (checkforcmd(&ea.cmd, "call", 3))
4304 {
4305 if (*ea.cmd == '(')
4306 // not for "call()"
4307 ea.cmd = p;
4308 else
4309 ea.cmd = skipwhite(ea.cmd);
4310 }
4311
4312 if (!starts_with_colon)
4313 {
4314 int assign;
4315
4316 // Check for assignment after command modifiers.
4317 assign = may_compile_assignment(&ea, &line, cctx);
4318 if (assign == OK)
4319 goto nextline;
4320 if (assign == FAIL)
4321 return FAIL;
4322 }
4323 }
4324
4325 /*
4326 * COMMAND after range
4327 * 'text'->func() should not be confused with 'a mark
4328 * 0z1234->func() should not be confused with a zero line number
4329 * "++nr" and "--nr" are eval commands
4330 * in "$ENV->func()" the "$" is not a range
4331 * "123->func()" is a method call
4332 */
4333 cmd = ea.cmd;
4334 if ((*cmd != '$' || starts_with_colon)
4335 && (starts_with_colon
4336 || !(*cmd == '\''
4337 || (cmd[0] == '0' && cmd[1] == 'z')
4338 || (cmd[0] != NUL && cmd[0] == cmd[1]
4339 && (*cmd == '+' || *cmd == '-'))
4340 || number_method(cmd))))
4341 {
4342 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
4343 if (ea.cmd > cmd)
4344 {
4345 if (!starts_with_colon
4346 && !(local_cmdmod.cmod_flags & CMOD_LEGACY))
4347 {
4348 semsg(_(e_colon_required_before_range_str), cmd);
4349 return FAIL;
4350 }
4351 ea.addr_count = 1;
4352 if (ends_excmd2(line, ea.cmd))
4353 {
4354 // A range without a command: jump to the line.
4355 generate_EXEC(cctx, ISN_EXECRANGE,
4356 vim_strnsave(cmd, ea.cmd - cmd));
4357 line = ea.cmd;
4358 goto nextline;
4359 }
4360 }
4361 }
4362 p = find_ex_command(&ea, NULL,
4363 starts_with_colon || (local_cmdmod.cmod_flags & CMOD_LEGACY)
4364 ? NULL : item_exists, cctx);
4365
4366 if (p == NULL)
4367 {
4368 if (cctx->ctx_skip != SKIP_YES)
4369 semsg(_(e_ambiguous_use_of_user_defined_command_str), ea.cmd);
4370 return FAIL;
4371 }
4372
4373 // When using ":legacy cmd" always use compile_exec().
4374 if (local_cmdmod.cmod_flags & CMOD_LEGACY)
4375 {
4376 char_u *start = ea.cmd;
4377
4378 switch (ea.cmdidx)
4379 {
4380 case CMD_if:
4381 case CMD_elseif:
4382 case CMD_else:
4383 case CMD_endif:
4384 case CMD_for:
4385 case CMD_endfor:
4386 case CMD_continue:
4387 case CMD_break:
4388 case CMD_while:
4389 case CMD_endwhile:
4390 case CMD_try:
4391 case CMD_catch:
4392 case CMD_finally:
4393 case CMD_endtry:
4394 semsg(_(e_cannot_use_legacy_with_command_str), ea.cmd);
4395 return FAIL;
4396 default: break;
4397 }
4398
4399 // ":legacy return expr" needs to be handled differently.
4400 if (checkforcmd(&start, "return", 4))
4401 ea.cmdidx = CMD_return;
4402 else
4403 ea.cmdidx = CMD_legacy;
4404 }
4405
4406 if (p == ea.cmd && ea.cmdidx != CMD_SIZE)
4407 {
4408 // "eval" is used for "val->func()" and "var" for "var = val", then
4409 // "p" is equal to "ea.cmd" for a valid command.
4410 if (ea.cmdidx == CMD_eval || ea.cmdidx == CMD_var)
4411 ;
4412 else if (cctx->ctx_skip == SKIP_YES)
4413 {
4414 line += STRLEN(line);
4415 goto nextline;
4416 }
4417 else
4418 {
4419 semsg(_(e_command_not_recognized_str), ea.cmd);
4420 return FAIL;
4421 }
4422 }
4423
4424 if ((cctx->ctx_had_return || cctx->ctx_had_throw)
4425 && ea.cmdidx != CMD_elseif
4426 && ea.cmdidx != CMD_else
4427 && ea.cmdidx != CMD_endif
4428 && ea.cmdidx != CMD_endfor
4429 && ea.cmdidx != CMD_endwhile
4430 && ea.cmdidx != CMD_catch
4431 && ea.cmdidx != CMD_finally
4432 && ea.cmdidx != CMD_endtry
4433 && !ignore_unreachable_code_for_testing)
4434 {
4435 semsg(_(e_unreachable_code_after_str),
4436 cctx->ctx_had_return ? "return" : "throw");
4437 return FAIL;
4438 }
Yegappan Lakshmananab9a8942024-12-30 09:56:34 +01004439
4440 // When processing the end of an if-else block, don't clear the
4441 // "ctx_had_throw" flag. If an if-else block ends in a "throw"
4442 // statement, then it is considered to end in a "return" statement.
4443 // The "ctx_had_throw" is cleared immediately after processing the
4444 // if-else block ending statement.
4445 // Otherwise, clear the "had_throw" flag.
4446 if (ea.cmdidx != CMD_else && ea.cmdidx != CMD_elseif
4447 && ea.cmdidx != CMD_endif)
4448 cctx->ctx_had_throw = FALSE;
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004449
4450 p = skipwhite(p);
4451 if (ea.cmdidx != CMD_SIZE
4452 && ea.cmdidx != CMD_write && ea.cmdidx != CMD_read)
4453 {
4454 if (ea.cmdidx >= 0)
4455 ea.argt = excmd_get_argt(ea.cmdidx);
4456 if ((ea.argt & EX_BANG) && *p == '!')
4457 {
4458 ea.forceit = TRUE;
4459 p = skipwhite(p + 1);
4460 }
4461 if ((ea.argt & EX_RANGE) == 0 && ea.addr_count > 0)
4462 {
4463 emsg(_(e_no_range_allowed));
4464 return FAIL;
4465 }
4466 }
4467
4468 switch (ea.cmdidx)
4469 {
4470 case CMD_def:
4471 case CMD_function:
4472 ea.arg = p;
4473 line = compile_nested_function(&ea, cctx, lines_to_free);
4474 break;
4475
4476 case CMD_return:
4477 line = compile_return(p, check_return_type,
4478 local_cmdmod.cmod_flags & CMOD_LEGACY, cctx);
4479 cctx->ctx_had_return = TRUE;
4480 break;
4481
4482 case CMD_let:
4483 emsg(_(e_cannot_use_let_in_vim9_script));
4484 break;
4485 case CMD_var:
4486 case CMD_final:
4487 case CMD_const:
4488 case CMD_increment:
4489 case CMD_decrement:
4490 line = compile_assignment(p, &ea, ea.cmdidx, cctx);
4491 if (line == p)
4492 {
4493 emsg(_(e_invalid_assignment));
4494 line = NULL;
4495 }
4496 break;
4497
4498 case CMD_unlet:
4499 case CMD_unlockvar:
4500 case CMD_lockvar:
4501 line = compile_unletlock(p, &ea, cctx);
4502 break;
4503
4504 case CMD_import:
4505 emsg(_(e_import_can_only_be_used_in_script));
4506 line = NULL;
4507 break;
4508
4509 case CMD_if:
4510 line = compile_if(p, cctx);
4511 break;
4512 case CMD_elseif:
4513 line = compile_elseif(p, cctx);
4514 cctx->ctx_had_return = FALSE;
Yegappan Lakshmananab9a8942024-12-30 09:56:34 +01004515 cctx->ctx_had_throw = FALSE;
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004516 break;
4517 case CMD_else:
4518 line = compile_else(p, cctx);
4519 cctx->ctx_had_return = FALSE;
Yegappan Lakshmananab9a8942024-12-30 09:56:34 +01004520 cctx->ctx_had_throw = FALSE;
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004521 break;
4522 case CMD_endif:
4523 line = compile_endif(p, cctx);
Yegappan Lakshmananab9a8942024-12-30 09:56:34 +01004524 cctx->ctx_had_throw = FALSE;
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004525 break;
4526
4527 case CMD_while:
4528 line = compile_while(p, cctx);
4529 break;
4530 case CMD_endwhile:
4531 line = compile_endwhile(p, cctx);
4532 cctx->ctx_had_return = FALSE;
4533 break;
4534
4535 case CMD_for:
4536 line = compile_for(p, cctx);
4537 break;
4538 case CMD_endfor:
4539 line = compile_endfor(p, cctx);
4540 cctx->ctx_had_return = FALSE;
4541 break;
4542 case CMD_continue:
4543 line = compile_continue(p, cctx);
4544 break;
4545 case CMD_break:
4546 line = compile_break(p, cctx);
4547 break;
4548
4549 case CMD_try:
4550 line = compile_try(p, cctx);
4551 break;
4552 case CMD_catch:
4553 line = compile_catch(p, cctx);
4554 cctx->ctx_had_return = FALSE;
4555 break;
4556 case CMD_finally:
4557 line = compile_finally(p, cctx);
4558 cctx->ctx_had_return = FALSE;
4559 break;
4560 case CMD_endtry:
4561 line = compile_endtry(p, cctx);
4562 break;
4563 case CMD_throw:
4564 line = compile_throw(p, cctx);
4565 cctx->ctx_had_throw = TRUE;
4566 break;
4567
4568 case CMD_eval:
4569 line = compile_eval(p, cctx);
4570 break;
4571
4572 case CMD_defer:
4573 line = compile_defer(p, cctx);
4574 break;
4575
4576#ifdef HAS_MESSAGE_WINDOW
4577 case CMD_echowindow:
4578 {
4579 long cmd_count = get_cmd_count(line, &ea);
4580 if (cmd_count < 0)
4581 line = NULL;
4582 else
4583 line = compile_mult_expr(p, ea.cmdidx,
4584 cmd_count, cctx);
4585 }
4586 break;
4587#endif
4588 case CMD_echo:
4589 case CMD_echon:
4590 case CMD_echoconsole:
4591 case CMD_echoerr:
4592 case CMD_echomsg:
4593 case CMD_execute:
4594 line = compile_mult_expr(p, ea.cmdidx, 0, cctx);
4595 break;
4596
4597 case CMD_put:
4598 ea.cmd = cmd;
4599 line = compile_put(p, &ea, cctx);
4600 break;
4601
4602 case CMD_substitute:
4603 if (check_global_and_subst(ea.cmd, p) == FAIL)
4604 return FAIL;
4605 if (cctx->ctx_skip == SKIP_YES)
4606 line = (char_u *)"";
4607 else
4608 {
4609 ea.arg = p;
4610 line = compile_substitute(line, &ea, cctx);
4611 }
4612 break;
4613
4614 case CMD_redir:
4615 ea.arg = p;
4616 line = compile_redir(line, &ea, cctx);
4617 break;
4618
4619 case CMD_cexpr:
4620 case CMD_lexpr:
4621 case CMD_caddexpr:
4622 case CMD_laddexpr:
4623 case CMD_cgetexpr:
4624 case CMD_lgetexpr:
4625#ifdef FEAT_QUICKFIX
4626 ea.arg = p;
4627 line = compile_cexpr(line, &ea, cctx);
4628#else
4629 ex_ni(&ea);
4630 line = NULL;
4631#endif
4632 break;
4633
4634 case CMD_append:
4635 case CMD_change:
4636 case CMD_insert:
4637 case CMD_k:
4638 case CMD_t:
4639 case CMD_xit:
4640 not_in_vim9(&ea);
4641 return FAIL;
4642
4643 case CMD_SIZE:
4644 if (cctx->ctx_skip != SKIP_YES)
4645 {
4646 semsg(_(e_invalid_command_str), ea.cmd);
4647 return FAIL;
4648 }
4649 // We don't check for a next command here.
4650 line = (char_u *)"";
4651 break;
4652
4653 case CMD_lua:
4654 case CMD_mzscheme:
4655 case CMD_perl:
4656 case CMD_py3:
4657 case CMD_python3:
4658 case CMD_python:
4659 case CMD_pythonx:
4660 case CMD_ruby:
4661 case CMD_tcl:
4662 ea.arg = p;
4663 if (vim_strchr(line, '\n') == NULL)
4664 line = compile_exec(line, &ea, cctx);
4665 else
4666 // heredoc lines have been concatenated with NL
4667 // characters in get_function_body()
4668 line = compile_script(line, cctx);
4669 break;
4670
4671 case CMD_vim9script:
4672 if (cctx->ctx_skip != SKIP_YES)
4673 {
4674 emsg(_(e_vim9script_can_only_be_used_in_script));
4675 return FAIL;
4676 }
4677 line = (char_u *)"";
4678 break;
4679
4680 case CMD_class:
4681 emsg(_(e_class_can_only_be_used_in_script));
4682 return FAIL;
4683
4684 case CMD_type:
4685 emsg(_(e_type_can_only_be_used_in_script));
4686 return FAIL;
4687
4688 case CMD_global:
4689 if (check_global_and_subst(ea.cmd, p) == FAIL)
4690 return FAIL;
4691 // FALLTHROUGH
4692 default:
4693 // Not recognized, execute with do_cmdline_cmd().
4694 ea.arg = p;
4695 line = compile_exec(line, &ea, cctx);
4696 break;
4697 }
4698nextline:
4699 if (line == NULL)
4700 return FAIL;
4701 line = skipwhite(line);
4702
4703 // Undo any command modifiers.
4704 generate_undo_cmdmods(cctx);
4705
4706 if (cctx->ctx_type_stack.ga_len < 0)
4707 {
4708 iemsg("Type stack underflow");
4709 return FAIL;
4710 }
4711 } // END of the loop over all the function body lines.
4712
4713 return OK;
4714}
4715
4716/*
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004717 * Returns TRUE if the end of a scope (if, while, for, block) is missing.
4718 * Called after compiling a def function body.
4719 */
4720 static int
4721compile_dfunc_scope_end_missing(cctx_T *cctx)
4722{
4723 if (cctx->ctx_scope == NULL)
4724 return FALSE;
4725
4726 if (cctx->ctx_scope->se_type == IF_SCOPE)
4727 emsg(_(e_missing_endif));
4728 else if (cctx->ctx_scope->se_type == WHILE_SCOPE)
4729 emsg(_(e_missing_endwhile));
4730 else if (cctx->ctx_scope->se_type == FOR_SCOPE)
4731 emsg(_(e_missing_endfor));
4732 else
4733 emsg(_(e_missing_rcurly));
4734
4735 return TRUE;
4736}
4737
4738/*
Yegappan Lakshmananab9a8942024-12-30 09:56:34 +01004739 * When compiling a def function, if it doesn't have an explicit return
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004740 * statement, then generate a default return instruction. For an object
4741 * constructor, return the object.
4742 */
4743 static int
4744compile_dfunc_generate_default_return(ufunc_T *ufunc, cctx_T *cctx)
4745{
4746 // TODO: if a function ends in "throw" but there was a return elsewhere we
4747 // should not assume the return type is "void".
4748 if (cctx->ctx_had_return || cctx->ctx_had_throw)
4749 return OK;
4750
4751 if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
4752 ufunc->uf_ret_type = &t_void;
4753 else if (ufunc->uf_ret_type->tt_type != VAR_VOID
4754 && !IS_CONSTRUCTOR_METHOD(ufunc))
4755 {
4756 emsg(_(e_missing_return_statement));
4757 return FAIL;
4758 }
4759
4760 // Return void if there is no return at the end.
4761 // For a constructor return the object.
4762 if (IS_CONSTRUCTOR_METHOD(ufunc))
4763 {
4764 generate_instr(cctx, ISN_RETURN_OBJECT);
4765 ufunc->uf_ret_type = &ufunc->uf_class->class_object_type;
4766 }
4767 else
4768 generate_instr(cctx, ISN_RETURN_VOID);
4769
4770 return OK;
4771}
4772
4773/*
4774 * Perform the chores after successfully compiling a def function.
4775 */
4776 static void
4777compile_dfunc_epilogue(
4778 cctx_T *outer_cctx,
4779 ufunc_T *ufunc,
4780 garray_T *instr,
4781 cctx_T *cctx)
4782{
4783 dfunc_T *dfunc;
4784
4785 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
4786 dfunc->df_deleted = FALSE;
4787 dfunc->df_script_seq = current_sctx.sc_seq;
4788
4789#ifdef FEAT_PROFILE
4790 if (cctx->ctx_compile_type == CT_PROFILE)
4791 {
4792 dfunc->df_instr_prof = instr->ga_data;
4793 dfunc->df_instr_prof_count = instr->ga_len;
4794 }
4795 else
4796#endif
4797 if (cctx->ctx_compile_type == CT_DEBUG)
4798 {
4799 dfunc->df_instr_debug = instr->ga_data;
4800 dfunc->df_instr_debug_count = instr->ga_len;
4801 }
4802 else
4803 {
4804 dfunc->df_instr = instr->ga_data;
4805 dfunc->df_instr_count = instr->ga_len;
4806 }
4807 dfunc->df_varcount = dfunc->df_var_names.ga_len;
4808 dfunc->df_has_closure = cctx->ctx_has_closure;
4809
4810 if (cctx->ctx_outer_used)
4811 {
4812 ufunc->uf_flags |= FC_CLOSURE;
4813 if (outer_cctx != NULL)
4814 ++outer_cctx->ctx_closure_count;
4815 }
4816
4817 ufunc->uf_def_status = UF_COMPILED;
4818}
4819
4820/*
4821 * Perform the cleanup when a def function compilation fails.
4822 */
4823 static void
4824compile_dfunc_ufunc_cleanup(
4825 ufunc_T *ufunc,
4826 garray_T *instr,
4827 int new_def_function,
4828 char *errormsg,
4829 int did_emsg_before,
4830 cctx_T *cctx)
4831{
4832 dfunc_T *dfunc;
4833
4834 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
4835
4836 // Compiling aborted, free the generated instructions.
4837 clear_instr_ga(instr);
4838 VIM_CLEAR(dfunc->df_name);
4839 ga_clear_strings(&dfunc->df_var_names);
4840
4841 // If using the last entry in the table and it was added above, we
4842 // might as well remove it.
4843 if (!dfunc->df_deleted && new_def_function
4844 && ufunc->uf_dfunc_idx == def_functions.ga_len - 1)
4845 {
4846 --def_functions.ga_len;
4847 ufunc->uf_dfunc_idx = 0;
4848 }
4849 ufunc->uf_def_status = UF_COMPILE_ERROR;
4850
4851 while (cctx->ctx_scope != NULL)
4852 drop_scope(cctx);
4853
4854 if (errormsg != NULL)
4855 emsg(errormsg);
4856 else if (did_emsg == did_emsg_before)
4857 emsg(_(e_compiling_def_function_failed));
4858}
4859
4860/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004861 * After ex_function() has collected all the function lines: parse and compile
4862 * the lines into instructions.
4863 * Adds the function to "def_functions".
Bram Moolenaar9e68c322020-12-25 12:38:04 +01004864 * When "check_return_type" is set then set ufunc->uf_ret_type to the type of
4865 * the return statement (used for lambda). When uf_ret_type is already set
4866 * then check that it matches.
Bram Moolenaarb2049902021-01-24 12:53:53 +01004867 * When "profiling" is true add ISN_PROF_START instructions.
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004868 * "outer_cctx" is set for a nested function.
Bram Moolenaar05afcee2020-03-31 23:32:31 +02004869 * This can be used recursively through compile_lambda(), which may reallocate
4870 * "def_functions".
Bram Moolenaar822ba242020-05-24 23:00:18 +02004871 * Returns OK or FAIL.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004872 */
Bram Moolenaar822ba242020-05-24 23:00:18 +02004873 int
Bram Moolenaarb2049902021-01-24 12:53:53 +01004874compile_def_function(
Bram Moolenaare99d4222021-06-13 14:01:26 +02004875 ufunc_T *ufunc,
4876 int check_return_type,
4877 compiletype_T compile_type,
4878 cctx_T *outer_cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004879{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004880 garray_T lines_to_free;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004881 char *errormsg = NULL; // error message
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004882 cctx_T cctx;
4883 garray_T *instr;
Bram Moolenaard66960b2020-10-30 20:46:26 +01004884 int did_emsg_before = did_emsg;
Bram Moolenaar599410c2021-04-10 14:03:43 +02004885 int did_emsg_silent_before = did_emsg_silent;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004886 int ret = FAIL;
4887 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02004888 int save_estack_compiling = estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02004889 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02004890 int do_estack_push;
Bram Moolenaar6c4bfe42020-07-23 18:26:30 +02004891 int new_def_function = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004892
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004893 // allocated lines are freed at the end
4894 ga_init2(&lines_to_free, sizeof(char_u *), 50);
4895
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004896 // Initialize the ufunc and the compilation context
4897 if (compile_dfunc_ufunc_init(ufunc, outer_cctx, compile_type,
4898 &new_def_function) == FAIL)
Bram Moolenaar96923b72022-03-15 15:57:04 +00004899 return FAIL;
Bram Moolenaar96923b72022-03-15 15:57:04 +00004900
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004901 compile_dfunc_cctx_init(&cctx, outer_cctx, ufunc, compile_type);
Bram Moolenaar985116a2020-07-12 17:31:09 +02004902
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004903 instr = &cctx.ctx_instr;
4904
Bram Moolenaar25e0f582020-05-25 22:36:50 +02004905 // Set the context to the function, it may be compiled when called from
4906 // another script. Set the script version to the most modern one.
4907 // The line number will be set in next_line_from_context().
4908 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004909 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
4910
Bram Moolenaardc4c2302021-04-25 13:54:42 +02004911 // Don't use the flag from ":legacy" here.
4912 cmdmod.cmod_flags &= ~CMOD_LEGACY;
4913
Bram Moolenaar25e0f582020-05-25 22:36:50 +02004914 // Make sure error messages are OK.
4915 do_estack_push = !estack_top_is_ufunc(ufunc, 1);
4916 if (do_estack_push)
4917 estack_push_ufunc(ufunc, 1);
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02004918 estack_compiling = TRUE;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02004919
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004920 // Make sure arguments don't shadow variables in the context
Bram Moolenaar9a015112021-12-31 14:06:45 +00004921 if (check_args_shadowing(ufunc, &cctx) == FAIL)
4922 goto erret;
4923
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004924 // For an object method and a constructor generate instructions to
4925 // initialize "this" and the object variables.
Bram Moolenaar574950d2023-01-03 19:08:50 +00004926 if (ufunc->uf_flags & (FC_OBJECT|FC_NEW))
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004927 if (obj_method_prologue(ufunc, &cctx) == FAIL)
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004928 goto erret;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004929
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004930 if (ufunc->uf_def_args.ga_len > 0)
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004931 if (compile_def_function_default_args(ufunc, instr, &cctx) == FAIL)
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004932 goto erret;
Bram Moolenaare28d9b32021-07-03 18:56:53 +02004933 ufunc->uf_args_visible = ufunc->uf_args.ga_len;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004934
Ernie Rael7c92e882025-01-18 17:26:39 +01004935 // Compiling an abstract method or a function in an interface is done to
4936 // get the function type. No code is actually compiled.
4937 if (ufunc->uf_class != NULL && (IS_INTERFACE(ufunc->uf_class)
4938 || IS_ABSTRACT_METHOD(ufunc)))
Bram Moolenaarb8bebd02023-01-30 20:24:23 +00004939 {
4940 ufunc->uf_def_status = UF_NOT_COMPILED;
4941 ret = OK;
4942 goto erret;
4943 }
4944
Yegappan Lakshmananf6c1fb22024-04-25 21:30:56 +02004945 // compile the function body
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004946 if (compile_def_function_body(ufunc->uf_lines.ga_len, check_return_type,
4947 &lines_to_free, &errormsg, &cctx) == FAIL)
Yegappan Lakshmanana16f2512024-04-23 20:14:46 +02004948 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004949
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004950 if (compile_dfunc_scope_end_missing(&cctx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004951 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004952
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004953 if (compile_dfunc_generate_default_return(ufunc, &cctx) == FAIL)
4954 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004955
Bram Moolenaar599410c2021-04-10 14:03:43 +02004956 // When compiled with ":silent!" and there was an error don't consider the
4957 // function compiled.
4958 if (emsg_silent == 0 || did_emsg_silent == did_emsg_silent_before)
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004959 compile_dfunc_epilogue(outer_cctx, ufunc, instr, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004960
4961 ret = OK;
4962
4963erret:
Bram Moolenaar599410c2021-04-10 14:03:43 +02004964 if (ufunc->uf_def_status == UF_COMPILING)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004965 {
Yegappan Lakshmananf07c10d2024-12-23 10:15:08 +01004966 // compilation failed. do cleanup.
4967 compile_dfunc_ufunc_cleanup(ufunc, instr, new_def_function,
4968 errormsg, did_emsg_before, &cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004969 }
4970
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004971 if (cctx.ctx_redir_lhs.lhs_name != NULL)
4972 {
4973 if (ret == OK)
4974 {
4975 emsg(_(e_missing_redir_end));
4976 ret = FAIL;
4977 }
4978 vim_free(cctx.ctx_redir_lhs.lhs_name);
Bram Moolenaar753bcf82021-04-21 14:24:24 +02004979 vim_free(cctx.ctx_redir_lhs.lhs_whole);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004980 }
4981
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004982 current_sctx = save_current_sctx;
Bram Moolenaarf4e8cdd2020-10-12 22:07:13 +02004983 estack_compiling = save_estack_compiling;
Bram Moolenaardc4c2302021-04-25 13:54:42 +02004984 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02004985 if (do_estack_push)
4986 estack_pop();
4987
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004988 ga_clear_strings(&lines_to_free);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02004989 free_locals(&cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004990 ga_clear(&cctx.ctx_type_stack);
Bram Moolenaar822ba242020-05-24 23:00:18 +02004991 return ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004992}
4993
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02004994 void
4995set_function_type(ufunc_T *ufunc)
4996{
4997 int varargs = ufunc->uf_va_name != NULL;
4998 int argcount = ufunc->uf_args.ga_len;
4999
5000 // Create a type for the function, with the return type and any
5001 // argument types.
5002 // A vararg is included in uf_args.ga_len but not in uf_arg_types.
5003 // The type is included in "tt_args".
5004 if (argcount > 0 || varargs)
5005 {
Bram Moolenaar18062fc2021-03-05 21:35:47 +01005006 if (ufunc->uf_type_list.ga_itemsize == 0)
5007 ga_init2(&ufunc->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005008 ufunc->uf_func_type = alloc_func_type(ufunc->uf_ret_type,
5009 argcount, &ufunc->uf_type_list);
5010 // Add argument types to the function type.
5011 if (func_type_add_arg_types(ufunc->uf_func_type,
5012 argcount + varargs,
5013 &ufunc->uf_type_list) == FAIL)
5014 return;
5015 ufunc->uf_func_type->tt_argcount = argcount + varargs;
5016 ufunc->uf_func_type->tt_min_argcount =
5017 argcount - ufunc->uf_def_args.ga_len;
5018 if (ufunc->uf_arg_types == NULL)
5019 {
5020 int i;
5021
5022 // lambda does not have argument types.
5023 for (i = 0; i < argcount; ++i)
5024 ufunc->uf_func_type->tt_args[i] = &t_any;
5025 }
5026 else
5027 mch_memmove(ufunc->uf_func_type->tt_args,
5028 ufunc->uf_arg_types, sizeof(type_T *) * argcount);
5029 if (varargs)
5030 {
5031 ufunc->uf_func_type->tt_args[argcount] =
Bram Moolenaar2a389082021-04-09 20:24:31 +02005032 ufunc->uf_va_type == NULL ? &t_list_any : ufunc->uf_va_type;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005033 ufunc->uf_func_type->tt_flags = TTFLAG_VARARGS;
5034 }
5035 }
5036 else
5037 // No arguments, can use a predefined type.
5038 ufunc->uf_func_type = get_func_type(ufunc->uf_ret_type,
5039 argcount, &ufunc->uf_type_list);
5040}
5041
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005042/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01005043 * Free all instructions for "dfunc" except df_name.
Bram Moolenaar20431c92020-03-20 18:39:46 +01005044 */
5045 static void
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005046delete_def_function_contents(dfunc_T *dfunc, int mark_deleted)
Bram Moolenaar20431c92020-03-20 18:39:46 +01005047{
5048 int idx;
5049
Bram Moolenaard505d172022-12-18 21:42:55 +00005050 // In same cases the instructions may refer to a class in which the
5051 // function is defined and unreferencing the class may call back here
5052 // recursively. Set the df_delete_busy to avoid problems.
5053 if (dfunc->df_delete_busy)
5054 return;
5055 dfunc->df_delete_busy = TRUE;
5056
Bram Moolenaar20431c92020-03-20 18:39:46 +01005057 ga_clear(&dfunc->df_def_args_isn);
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005058 ga_clear_strings(&dfunc->df_var_names);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005059
5060 if (dfunc->df_instr != NULL)
5061 {
5062 for (idx = 0; idx < dfunc->df_instr_count; ++idx)
5063 delete_instr(dfunc->df_instr + idx);
5064 VIM_CLEAR(dfunc->df_instr);
5065 }
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +02005066 if (dfunc->df_instr_debug != NULL)
5067 {
5068 for (idx = 0; idx < dfunc->df_instr_debug_count; ++idx)
5069 delete_instr(dfunc->df_instr_debug + idx);
5070 VIM_CLEAR(dfunc->df_instr_debug);
Bram Moolenaarc2dec4c2021-06-13 15:39:00 +02005071 }
Bram Moolenaarc05fe072021-01-24 21:30:48 +01005072#ifdef FEAT_PROFILE
5073 if (dfunc->df_instr_prof != NULL)
5074 {
5075 for (idx = 0; idx < dfunc->df_instr_prof_count; ++idx)
5076 delete_instr(dfunc->df_instr_prof + idx);
5077 VIM_CLEAR(dfunc->df_instr_prof);
Bram Moolenaarc05fe072021-01-24 21:30:48 +01005078 }
5079#endif
Bram Moolenaar20431c92020-03-20 18:39:46 +01005080
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005081 if (mark_deleted)
5082 dfunc->df_deleted = TRUE;
5083 if (dfunc->df_ufunc != NULL)
5084 dfunc->df_ufunc->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaard505d172022-12-18 21:42:55 +00005085
5086 dfunc->df_delete_busy = FALSE;
Bram Moolenaar20431c92020-03-20 18:39:46 +01005087}
5088
5089/*
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005090 * When a user function is deleted, clear the contents of any associated def
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01005091 * function, unless another user function still uses it.
5092 * The position in def_functions can be re-used.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005093 */
5094 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01005095unlink_def_function(ufunc_T *ufunc)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005096{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00005097 if (ufunc->uf_dfunc_idx <= 0)
5098 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005099
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00005100 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5101 + ufunc->uf_dfunc_idx;
5102
5103 if (--dfunc->df_refcount <= 0)
5104 delete_def_function_contents(dfunc, TRUE);
5105 ufunc->uf_def_status = UF_NOT_COMPILED;
5106 ufunc->uf_dfunc_idx = 0;
5107 if (dfunc->df_ufunc == ufunc)
5108 dfunc->df_ufunc = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005109}
5110
Bram Moolenaarfdeab652020-09-19 15:16:50 +02005111/*
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01005112 * Used when a user function refers to an existing dfunc.
Bram Moolenaarfdeab652020-09-19 15:16:50 +02005113 */
5114 void
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01005115link_def_function(ufunc_T *ufunc)
Bram Moolenaarfdeab652020-09-19 15:16:50 +02005116{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00005117 if (ufunc->uf_dfunc_idx <= 0)
5118 return;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02005119
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00005120 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5121 + ufunc->uf_dfunc_idx;
5122
5123 ++dfunc->df_refcount;
Bram Moolenaarfdeab652020-09-19 15:16:50 +02005124}
5125
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005126#if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar20431c92020-03-20 18:39:46 +01005127/*
5128 * Free all functions defined with ":def".
5129 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005130 void
5131free_def_functions(void)
5132{
Bram Moolenaar20431c92020-03-20 18:39:46 +01005133 int idx;
5134
5135 for (idx = 0; idx < def_functions.ga_len; ++idx)
5136 {
5137 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + idx;
5138
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005139 delete_def_function_contents(dfunc, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01005140 vim_free(dfunc->df_name);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005141 }
5142
5143 ga_clear(&def_functions);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005144}
5145#endif
5146
5147
5148#endif // FEAT_EVAL